欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中通用的线程池实例代码

程序员文章站 2023-11-24 14:50:40
复制代码 代码如下:package com.smart.frame.task.autotask; import java.util.collection;import j...

复制代码 代码如下:

package com.smart.frame.task.autotask;

import java.util.collection;
import java.util.vector;

/**
 * 任务分发器
 */
public class taskmanage extends thread
{
    protected vector<runnable> tasks = new vector<runnable>();
    protected boolean running = false;
    protected boolean stopped = false;
    protected boolean paused = false;
    protected boolean killed = false;
    private threadpool pool;

    public taskmanage(threadpool pool)
    {
        this.pool = pool;
    }

    public void puttask(runnable task)
    {
        tasks.add(task);
    }

    public void puttasks(runnable[] tasks)
    {
        for (int i = 0; i < tasks.length; i++)
            this.tasks.add(tasks[i]);
    }

    public void puttasks(collection<runnable> tasks)
    {
        this.tasks.addall(tasks);
    }

    protected runnable poptask()
    {
        if (tasks.size() > 0) return (runnable) tasks.remove(0);
        else return null;
    }

    public boolean isrunning()
    {
        return running;
    }

    public void stoptasks()
    {
        stopped = true;
    }

    public void stoptaskssync()
    {
        stoptasks();
        while (isrunning())
        {
            try
            {
                sleep(5);
            }
            catch (interruptedexception e)
            {
                taskexception.getresultmessage(e);
            }
        }
    }

    public void pausetasks()
    {
        paused = true;
    }

    public void pausetaskssync()
    {
        pausetasks();
        while (isrunning())
        {
            try
            {
                sleep(5);
            }
            catch (interruptedexception e)
            {
                taskexception.getresultmessage(e);
            }
        }
    }

    public void kill()
    {
        if (!running) interrupt();
        else killed = true;
    }

    public void killsync()
    {
        kill();
        while (isalive())
        {
            try
            {
                sleep(5);
            }
            catch (interruptedexception e)
            {
                taskexception.getresultmessage(e);
            }
        }
    }

    public synchronized void starttasks()
    {
        running = true;
        this.notify();
    }

    public synchronized void run()
    {
        try
        {
            while (true)
            {
                if (!running || tasks.size() == 0)
                {
                    pool.notifyforidlethread();
                    this.wait();
                }
                else
                {
                    runnable task;
                    while ((task = poptask()) != null)
                    {
                        task.run();
                        if (stopped)
                        {
                            stopped = false;
                            if (tasks.size() > 0)
                            {
                                tasks.clear();
                                system.out.println(thread.currentthread().getid() + ": tasks are stopped");
                                break;
                            }
                        }
                        if (paused)
                        {
                            paused = false;
                            if (tasks.size() > 0)
                            {
                                system.out.println(thread.currentthread().getid() + ": tasks are paused");
                                break;
                            }
                        }
                    }
                    running = false;
                }

                if (killed)
                {
                    killed = false;
                    break;
                }
            }
        }
        catch (interruptedexception e)
        {
            taskexception.getresultmessage(e);
            return;
        }
    }
}

复制代码 代码如下:

package com.smart.frame.task.autotask;

import java.util.collection;
import java.util.iterator;
import java.util.vector;

/**
 * 线程池
 */
public class threadpool
{
    protected int maxpoolsize = taskconfig.maxpoolsize;
    protected int initpoolsize = taskconfig.initpoolsize;
    protected vector<taskmanage> threads = new vector<taskmanage>();
    protected boolean initialized = false;
    protected boolean hasidlethread = false;

    public threadpool()
    {
        super();
    }

    public threadpool(int maxpoolsize, int initpoolsize)
    {
        this.maxpoolsize = maxpoolsize;
        this.initpoolsize = initpoolsize;
    }

    public void init()
    {
        initialized = true;
        for (int i = 0; i < initpoolsize; i++)
        {
            taskmanage thread = new taskmanage(this);
            thread.start();
            threads.add(thread);
        }
    }

    public void setmaxpoolsize(int maxpoolsize)
    {
        this.maxpoolsize = maxpoolsize;
        if (maxpoolsize < getpoolsize()) setpoolsize(maxpoolsize);
    }

    /**
     * 重设当前线程数 若需杀掉某线程,线程不会立刻杀掉,而会等到线程中的事
     * 务处理完成 但此方法会立刻从线程池中移除该线程,不会等待事务处理结束
     */
    public void setpoolsize(int size)
    {
        if (!initialized)
        {
            initpoolsize = size;
            return;
        }
        else if (size > getpoolsize())
        {
            for (int i = getpoolsize(); i < size && i < maxpoolsize; i++)
            {
                taskmanage thread = new taskmanage(this);
                thread.start();
                threads.add(thread);
            }
        }
        else if (size < getpoolsize())
        {
            while (getpoolsize() > size)
            {
                taskmanage th = (taskmanage) threads.remove(0);
                th.kill();
            }
        }
    }

    public int getpoolsize()
    {
        return threads.size();
    }

    protected void notifyforidlethread()
    {
        hasidlethread = true;
    }

    protected boolean waitforidlethread()
    {
        hasidlethread = false;
        while (!hasidlethread && getpoolsize() >= maxpoolsize)
        {
            try
            {
                thread.sleep(5);
            }
            catch (interruptedexception e)
            {
                taskexception.getresultmessage(e);
                return false;
            }
        }

        return true;
    }

    public synchronized taskmanage getidlethread()
    {
        while (true)
        {
            for (iterator<taskmanage> itr = threads.iterator(); itr.hasnext();)
            {
                taskmanage th = (taskmanage) itr.next();
                if (!th.isrunning()) return th;
            }

            if (getpoolsize() < maxpoolsize)
            {
                taskmanage thread = new taskmanage(this);
                thread.start();
                threads.add(thread);
                return thread;
            }

            if (waitforidlethread() == false) return null;
        }
    }

    public void processtask(runnable task)
    {
        taskmanage th = getidlethread();
        if (th != null)
        {
            th.puttask(task);
            th.starttasks();
        }
    }

    public void processtasksinsinglethread(runnable[] tasks)
    {
        taskmanage th = getidlethread();
        if (th != null)
        {
            th.puttasks(tasks);
            th.starttasks();
        }
    }

    public void processtasksinsinglethread(collection<runnable> tasks)
    {
        taskmanage th = getidlethread();
        if (th != null)
        {
            th.puttasks(tasks);
            th.starttasks();
        }
    }

}

复制代码 代码如下:

package com.smart.frame.task.autotask;

public class toptask implements runnable
{

    private threadpool pool;

    public toptask()
    {
        super();
    }

    public toptask(threadpool pool)
    {
        super();
        this.pool = pool;
    }

    @override
    public void run()
    {
        init();
        start();
    }

    /**
     * 初始化验证权限、参数之类
     */
    public void init()
    {

    }

    /**
     * 开始自动任务
     */
    public void start()
    {
        for (int i = 0; i < 10; i++)
        {
            pool.processtask(new beginauto());
        }
    }
}
/**
 * 实现类
 */
class beginauto implements runnable
{
    @override
    public void run()
    {
        system.out.println(thread.currentthread().getid() + "..................");
    }

}