java ThreadPool 线程池

package com.karl.threadpool;

public abstract class MyTask implements Runnable{
    protected abstract boolean needRunImmde();
    public void run(){
        
    }
}

package com.karl.threadpool;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class MyThreadPool { List<MyTask> tasks = Collections.synchronizedList(new ArrayList<MyTask>()); private static MyThreadPool instance = MyThreadPool.getInstance(); public static MyThreadPool getInstance() { if (instance == null) { instance = new MyThreadPool(); } return instance; } private WorkThread[] threads; private MyThreadPool() { threads = new WorkThread[10]; for (int i = 0; i < 10; i++) { threads[i] = new WorkThread(); } } public void addTask(MyTask task) { synchronized (tasks) { tasks.add(task); tasks.notifyAll(); } } class WorkThread extends Thread { public WorkThread() { start(); } @Override public void run() { while (true) { synchronized (tasks) { if (tasks.isEmpty()) { try { // When call the method wait or notify of a Object, // must ensure the Object is synchronized. // Here let the Thread which use the object wait a // while. tasks.wait(20); continue; } catch (InterruptedException e) { } } MyTask r = tasks.remove(0); if (r != null) { if (r.needRunImmde()) { new Thread(r).start(); } else { r.run(); } } } } } }}

package com.karl.threadpool;

public class PrintNumber extends MyTask {
    
    private int index;
    private boolean needRunImmde=false;
    
    public boolean isNeedRunImmde() {
        return needRunImmde;
    }

    public void setNeedRunImmde(boolean needRunImmde) {
        this.needRunImmde = needRunImmde;
    }

    public PrintNumber(int index){
        this.index=index;
    }

    @Override
    protected boolean needRunImmde() {
        // TODO Auto-generated method stub
        return needRunImmde;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(index);
    }

}

package com.karl.threadpool;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        
        for (int i = 0; i < 10; i++) {
            PrintNumber pn = new PrintNumber(i);
            if((i%2)==0){
                pn.setNeedRunImmde(false);
            }
            
            MyThreadPool.getInstance().addTask(pn);
        }
        Thread.sleep(10000L);
        for (int i = 0; i < 10; i++) {
            PrintNumber pn = new PrintNumber(i);
            if((i%2)==0){
                pn.setNeedRunImmde(false);
            }
            
            MyThreadPool.getInstance().addTask(pn);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值