【java】【10】線程池使用bug,把task.get()放到循環裏無法保證同步

1.bugdemo

package com.zhenzhen;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
	
	private static ExecutorService executorPool;

	static {
		executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		long startTime = System.currentTimeMillis();
		for(int i=0;i<3;i++) {
			FutureTask<Integer> task = new FutureTask<Integer>(new CleanRedisZsetTask());
			executorPool.execute(task);
			
			System.out.println(task.get());
			
		}
		System.out.println("耗時"+(System.currentTimeMillis()-startTime)/1000);
	}
	
}

class CleanRedisZsetTask implements Callable<Integer> {

	@Override
	public Integer call() throws Exception {
		Thread.sleep(3000);
		return 1;
	}
	
}

2.正確使用

package com.brcloud;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
	
	private static ExecutorService executorPool;

	static {
		executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		long startTime = System.currentTimeMillis();
		List<Future<Integer>> taskList = new ArrayList<>();
		for(int i=0;i<3;i++) {

			taskList.add(executorPool.submit(new CleanRedisZsetTask()));
			//System.out.println(task.get());
			
		}
		
		for(Future<Integer> task:taskList) {
			task.get();
		}
		
		System.out.println("耗時"+(System.currentTimeMillis()-startTime)/1000);
	}
	
}

class CleanRedisZsetTask implements Callable<Integer> {

	@Override
	public Integer call() throws Exception {
		Thread.sleep(3000);
		return 1;
	}
	
}

3.如何卻出線程中的變量

package com.brcloud;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
	
	private static ExecutorService executorPool;

	static {
		executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		long startTime = System.currentTimeMillis();
		List<Future<Integer>> taskList = new ArrayList<>();
		List<CleanRedisZsetTask> tasks = new ArrayList<>();
		for(int i=0;i<3;i++) {
			CleanRedisZsetTask cleanRedisZsetTask = new CleanRedisZsetTask("張三"+i,23);
			tasks.add(cleanRedisZsetTask);
			taskList.add(executorPool.submit(cleanRedisZsetTask));
			//System.out.println(task.get());
			
		}
		
		int i = 0;
		for(CleanRedisZsetTask  cleanRedisZsetTask:tasks) {
			System.out.println(cleanRedisZsetTask);
			taskList.get(i).get();
			i++;
		}
		
		System.out.println("耗時"+(System.currentTimeMillis()-startTime)/1000);
	}
	
}

class CleanRedisZsetTask implements Callable<Integer> {
	
	private String name;
	private int age;
	
	

	@Override
	public String toString() {
		return "CleanRedisZsetTask [name=" + name + ", age=" + age + "]";
	}


	public CleanRedisZsetTask(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}



	@Override
	public Integer call() throws Exception {
		Thread.sleep(3000);
		return 1;
	}
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章