1. 程式人生 > >java執行緒池中以程式碼的順序執行,主要是記錄一下繼承執行緒池的內容

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(); } } } } }

好了,我主要是記錄給自己用的,有興趣的自己看程式碼吧.

實際上,只要執行緒池只有一個容量,就一定是順序執行的.與我程式碼中的刪除無關.