Max2060 2015-12-21 07:51 采纳率: 0%
浏览 3018

多个多线程类按顺序执行

第一次玩多线程,看代码。
public class Test {
public static void doSomthing(){
System.out.println("11111111111");
}

public static void main(String[] args) {

    System.out.println("调用线程1方法...");
    TestThread1 testThread1 = new TestThread1();
    testThread1.doSomething();

    System.out.println("调用线程2方法...");
    TestThread2 testThread2 = new TestThread2();
    testThread2.doSomething();

    System.out.println("其他方法.....");
    doSomthing();
}

}
需求是:main里面的3个方法,要按顺序执行。

public class TestThread1 implements Runnable{

public void doSomething(){
    ThreadPool pool = new ThreadPool();
    for (int i = 0; i < 10; i++) {
        pool.addThread(new TestThread1());
    }
    pool.shutdonw();
}

public void run() {
    System.out.println("这是线程1...");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}
TestThread1 和 TestThread2 是一样的。

public class ThreadPool {
private Integer POOL_SIZE = 2;
private ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);
private ThreadCallback callback = null;
private ThreadMonitor monitor = null;

public ThreadPool() {
}

public synchronized void addThread(Runnable thread) {
    pool.execute(thread);
}

public synchronized void setCallback(ThreadCallback callback) {
    if (monitor == null) {
        this.callback = callback;
        monitor = new ThreadMonitor(pool, callback);
        Thread thread = new Thread(monitor);
        thread.start();
    }
}

public synchronized void shutdonw() {
    pool.shutdown();
}

public synchronized boolean isTerminated() {
    return pool.isTerminated();
}

}
ThreadPool是别人写的,
求大神啊!~~

需求是:main里面的3个方法,要按顺序执行。
就是
testThread1.doSomething();
完成后执行testThread2.doSomething();
完成后执行doSomthing();

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-12-21 12:27
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码