您的位置:首页 > 编程语言 > Java开发

两个带有线程池的方法分别执行两组线程任务,第二个方法需要等待第一组任务执行完毕

2016-08-17 09:47 387 查看
如下两种解决方案其中一个会影响效率,另一个可能会有未知风险,如果谁有更好的解决方案,谢谢分享。

package thread;

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.Executors;
import java.util.concurrent.FutureTask;

public class CallableAndFuture {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<FutureTask<Integer>> taskList=method1();
for(FutureTask<Integer> task:taskList){
task.get();
}
System.out.println();
System.out.println("*****任务一******全部执行结束****");
System.out.println();
method2();
System.out.println();
System.out.println("………………任务二…………是否执行结束?????,一般情况还没有哦!");
System.out.println();
}

public static List<FutureTask<Integer>> method1(){
ExecutorService executor = Executors.newCachedThreadPool();
List<FutureTask<Integer>> taskList=new ArrayList<FutureTask<Integer>>();
for(int i=0;i<5;i++){
TheadTask1 task = new TheadTask1();
FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
executor.submit(futureTask);
taskList.add(futureTask);
}
return taskList;
}

public static List<FutureTask<Integer>> method2(){
ExecutorService executor = Executors.newCachedThreadPool();
List<FutureTask<Integer>> taskList=new ArrayList<FutureTask<Integer>>();
for(int i=0;i<5;i++){
TheadTask2 task = new TheadTask2();
FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
executor.submit(futureTask);
taskList.add(futureTask);
}
return taskList;
}
}

class TheadTask1 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("Task11111111111111111111111111111开始工作");
Thread.sleep((long) (Math.random() * 6000));
System.out.println("Task11111111111111111111111111111----结束工作"+Thread.currentThread().getName());
return 1;
}
}

class TheadTask2 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("Task22222222222222222222222222222开始工作");
Thread.sleep((long) (Math.random() * 6000));
System.out.println("Task22222222222222222222222222222----结束工作"+Thread.currentThread().getName());
return 1;
}
}


第二种方案:

package thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaPhore {
private static final Semaphore semp = new Semaphore(1);
public static void main(String[] args) throws InterruptedException {
method();
}

public static void method() throws InterruptedException{
method1();
//Thread.sleep(1000*2);
method2();
}

public static void method1() throws InterruptedException{
semp.release();
ExecutorService exec = Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
Task1 task1=new Task1();
exec.execute(task1);
}
// 退出线程池
exec.shutdown();
System.out.println("method1");
while (true) {
if (exec.isTerminated()) {
System.out.println("--method111--准备释放信号灯");
semp.acquire();
break;
}
Thread.sleep(200);
}
//
}

public static void method2() throws InterruptedException{
semp.release();
// 线程池
ExecutorService exec = Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
Task2 task2=new Task2();
exec.execute(task2);
}
// 退出线程池
exec.shutdown();
while (true) {
if (exec.isTerminated()) {
System.out.println("--method222--准备释放信号灯");
semp.acquire();
break;
}
Thread.sleep(200);
}
}
}
class Task1 implements Runnable{

@Override
public void run() {
try {
System.out.println("Task11111111111111111111111111111开始工作");
Thread.sleep((long) (Math.random() * 6000));
System.out.println("Task11111111111111111111111111111----结束工作"+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
class Task2 implements Runnable{

@Override
public void run() {
try {
System.out.println("Task22222222222222222222222222222开始工作");
Thread.sleep((long) (Math.random() * 6000));
System.out.println("Task22222222222222222222222222222----结束工作"+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 线程池 线程