java线程池中以代码的顺序运行,主要是记录一下继承线程池的内容

1.这个是自定义的线程池类,直接上代码

package org.jimmy.threadtest20181121;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutor201811281311 extends ThreadPoolExecutor {

    public static Runnable prevRunnable;
    
    public ThreadPoolExecutor201811281311(int corePoolSize,
            int maximumPoolSize, long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        if(prevRunnable != null){
            super.remove(prevRunnable);
        }
        super.beforeExecute(t, r);
    }

}

2.多线程操作的测试类,直接上代码

package org.jimmy.threadtest20181121;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread201811271616 {

    public static Semaphore semaphore = new Semaphore(1, true);
//    public static Semaphore semaphore = new Semaphore(1000, true);
    
    public static void main(String[] args) {
        try {
            long beginTime = new Date().getTime();
            TestThread201811271616 testThread201811271616 = new TestThread201811271616();
            LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
            ThreadPoolExecutor201811281311 executor = new ThreadPoolExecutor201811281311(1, 10000, 3600, TimeUnit.SECONDS, workQueue);
            /*ExecutorService executorService = Executors.newSingleThreadExecutor();
            for(int i = 0 ; i < 30; i++) {
                String id = i + "";
                Thread thread = new Thread(testThread201811271616.new LineUpThread(id));
                executorService.submit(thread);
            }
            executorService.shutdown();*/
            for(int i = 0 ; i < 1000; i++) {
                String id = i + "";
                Runnable runnable = testThread201811271616.new LineUpThread(id);
                Runnable prevRunnable = null;
                if(i > 0){
                    String prevId = (i - 1) + "";
                    prevRunnable = testThread201811271616.new LineUpThread(prevId);
                    ThreadPoolExecutor201811281311.prevRunnable = prevRunnable;
                }
                executor.execute(runnable);
            }
            executor.shutdown();
            while(!executor.isTerminated()){
                Thread.sleep(1);
            }
            long endTime = new Date().getTime();
            System.out.println("一共耗时:" + (endTime - beginTime) + "毫秒!");
            /*semaphore.acquireUninterruptibly();
            System.out.println("工作人员吃饭了,暂停服务!");
            semaphore.release();*/
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    class LineUpThread implements Runnable {

        private String id;
        
        public LineUpThread(String id) {
            this.id = id;
        }
        
        @Override
        public void run() {
            synchronized(this) {
                try {
                    semaphore.acquireUninterruptibly();
//                    System.out.println("id:" + id);
                    System.out.println("轮到编号" + id + "的客户了,可以开始购票了!");
                    System.out.println("编号" + id + "的客户已购票成功!");
                    semaphore.release();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

}

好了,我主要是记录给自己用的,有兴趣的自己看代码吧.

实际上,只要线程池只有一个容量,就一定是顺序执行的.与我代码中的删除无关.

猜你喜欢

转载自www.cnblogs.com/JimmySeraph/p/10031776.html