Java 跟踪执行线程

Java 跟踪执行线程,java,multithreading,concurrency,Java,Multithreading,Concurrency,我试图弄清楚如何跟踪我的应用程序正在生成的所有线程。起初,我以为我已经用CyclicBarrier解决了这个问题,但是我看到线程在我的等待调用后执行 下面是正在工作的伪代码: public class ThreadTesterRunner { public static void main(String[] args) throws InterruptedException { final CyclicBarrier cb = new CyclicBarrier(1)

我试图弄清楚如何跟踪我的应用程序正在生成的所有线程。起初,我以为我已经用CyclicBarrier解决了这个问题,但是我看到线程在我的等待调用后执行

下面是正在工作的伪代码:

public class ThreadTesterRunner {

    public static void main(String[] args) throws InterruptedException {

        final CyclicBarrier cb = new CyclicBarrier(1);
        ThreadRunner tr = new ThreadRunner(cb);
        Thread t = new Thread(tr, "Thread Runner");
        t.start();

        boolean process = true;
        // wait until all threads process, then print reports
        while (process){
            if(tr.getIsFinished()){
                System.out.println("Print metrics");
                process = false;
            }
            Thread.sleep(1000);
        }
    }
}


class ThreadRunner implements Runnable {
    static int timeOutTime = 2;
    private ExecutorService executorService = Executors.newFixedThreadPool(10);
    private final CyclicBarrier barrier;
    private boolean isFinished=false;

    public ThreadRunner(CyclicBarrier cb) {
        this.barrier = cb;
    }

    public void run(){
        try {
            boolean stillLoop = true; int i = 0;
            while (stillLoop){
                int size;
                Future<Integer> future = null;
                try {
                    future = executorService.submit(new Reader()); // sleeps
                    size = future.get();
                } catch (InterruptedException | ExecutionException ex) {
                    // handle Errs
                }

                if(i == 3){
                    stillLoop = false;
                    this.barrier.await();
                    this.isFinished=true;
                }
                //System.out.println("i = "+i+"  Size is: "+size+"\r");
                i++;
            }
        } catch (InterruptedException | BrokenBarrierException e1) {
            e1.printStackTrace();
        }
    }

    public boolean getIsFinished(){
        return this.isFinished;
    }
}

class Reader implements Callable {
    private ExecutorService executorService = Executors.newFixedThreadPool(1);

    @Override
    public Object call() throws Exception {
        System.out.println("Reading...");
        Thread.sleep(2000);
        executorService.submit(new Writer());
        return 1000;
    }
}

class Writer implements Callable {
    @Override
    public Void call() throws Exception {
        Thread.sleep(4000);
        System.out.println("Wrote");    
        return null;
    }
}
公共类ThreadTesterRunner{
公共静态void main(字符串[]args)引发InterruptedException{
最终循环载体cb=新循环载体(1);
ThreadRunner tr=新的ThreadRunner(cb);
螺纹t=新螺纹(tr,“螺纹流道”);
t、 start();
布尔过程=真;
//等待所有线程处理,然后打印报告
while(过程){
if(tr.getIsFinished()){
System.out.println(“打印度量”);
过程=假;
}
睡眠(1000);
}
}
}
类ThreadRunner实现可运行{
静态int timeOutTime=2;
私有ExecutorService ExecutorService=Executors.newFixedThreadPool(10);
私人自行车终点障碍;
私有布尔值isFinished=false;
公共ThreadRunner(CyclicBarrier cb){
这个障碍=cb;
}
公开募捐{
试一试{
布尔stillLoop=true;int i=0;
while(静止循环){
整数大小;
Future=null;
试一试{
future=executorService.submit(new Reader());//休眠
size=future.get();
}捕获(InterruptedException | ExecutionException ex){
//处理错误
}
如果(i==3){
stillLoop=false;
这个。障碍。等待();
this.isFinished=true;
}
//System.out.println(“i=“+i+”大小为:“+Size+”\r”);
i++;
}
}捕获(中断异常|断开的异常e1){
e1.printStackTrace();
}
}
公共布尔getIsFinished(){
返回此文件。已完成;
}
}
类读取器实现了可调用{
私有ExecutorService ExecutorService=Executors.newFixedThreadPool(1);
@凌驾
公共对象调用()引发异常{
System.out.println(“读取…”);
《睡眠》(2000年);
executorService.submit(新编写器());
返回1000;
}
}
类编写器实现了可调用的{
@凌驾
public Void call()引发异常{
睡眠(4000);
System.out.println(“写入”);
返回null;
}
}

有人能建议一种只在所有线程运行后打印“打印度量”的方法吗?

从我所看到的情况来看,您并不是在等待
编写器在
读取器中完成。这就是你看到的问题吗

您还可以从多个线程访问
isFinished
,而不进行同步(但是,在这种情况下,这只会延迟循环的终止)

我看不到骑自行车的人做任何事

我不知道你想做什么,但我会想我能做得多简单。例如,读写器可以合并到一个任务中吗?那么,等待他们完成仅仅是:

executorService.invokeAll(tasks);
System.out.println("Print metrics");

其中,
tasks
是一组任务(另请参见)

您似乎没有做任何事情来协调您想要等待的
阅读器
编写器
线程。如果您将同步障碍传递给这些线程,以便它们可以注册并在完成时发出信号,那么它就可以正常工作

这是一个重写版本,使用
相位器而不是
周期载波器来实现这一点。请注意,每个
读卡器
写卡器
在构造时都会注册自身,并在执行完成时通知同步屏障:

public class ThreadTesterRunner {
    public static void main(String[] args) throws InterruptedException {
        final Phaser cb = new Phaser();
        ThreadRunner tr = new ThreadRunner(cb);
        Thread t = new Thread(tr, "Thread Runner");
        t.start();

        boolean process = true;
        // wait until all threads process, then print reports
        while (process){
            if(tr.getIsFinished()){
                System.out.println("Print metrics");
                process = false;
            }
            //else {
            //  System.out.println("Waiting:  registered=" + cb.getRegisteredParties() + ", arrived=" + cb.getArrivedParties() + ", unarrived=" + cb.getUnarrivedParties());
            //}
            Thread.sleep(1000);
        }
    }
}


class ThreadRunner implements Runnable {
    static int timeOutTime = 2;
    private ExecutorService executorService = Executors.newFixedThreadPool(10);
    private final Phaser barrier;
    private boolean isFinished=false;

    public ThreadRunner(Phaser phaser) {
        this.barrier = phaser;
    }

    public void run(){
        try {
            boolean stillLoop = true; int i = 0;
            while (stillLoop){
                int size;
                Future<Integer> future = null;
                try {
                    future = executorService.submit(new Reader(this.barrier)); // sleeps
                    size = future.get();
                } catch (InterruptedException | ExecutionException ex) {
                    // handle Errs
                }

                if(i == 3){
                    stillLoop = false;
                    this.barrier.awaitAdvance(0);
                    this.isFinished=true;
                }
                //System.out.println("i = "+i+"  Size is: "+size+"\r");
                i++;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public boolean getIsFinished(){
        return this.isFinished;
    }
}

class Reader implements Callable {
    private Phaser barrier;
    private ExecutorService executorService = Executors.newFixedThreadPool(1);

    public Reader(Phaser phase) {
        phase.register();
        this.barrier = phase;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("Reading...");
        Thread.sleep(2000);
        executorService.submit(new Writer(this.barrier));
        this.barrier.arrive();
        return 1000;
    }
}

class Writer implements Callable {
    private Phaser barrier;

    public Writer(Phaser phase) {
        phase.register();
        this.barrier = phase;
    }

    @Override
    public Void call() throws Exception {
        Thread.sleep(4000);
        System.out.println("Wrote");
        this.barrier.arrive();
        return null;
    }
}
公共类ThreadTesterRunner{
公共静态void main(字符串[]args)引发InterruptedException{
最终相位器cb=新相位器();
ThreadRunner tr=新的ThreadRunner(cb);
螺纹t=新螺纹(tr,“螺纹流道”);
t、 start();
布尔过程=真;
//等待所有线程处理,然后打印报告
while(过程){
if(tr.getIsFinished()){
System.out.println(“打印度量”);
过程=假;
}
//否则{
//System.out.println(“Waiting:registered=“+cb.getRegisteredParties()+”,arrived=“+cb.getArrivedParties()+”,unarrived=“+cb.getUnarrivedParties()”);
//}
睡眠(1000);
}
}
}
类ThreadRunner实现可运行{
静态int timeOutTime=2;
私有ExecutorService ExecutorService=Executors.newFixedThreadPool(10);
专用最终相位器屏障;
私有布尔值isFinished=false;
公共螺纹流道(相位器相位器){
该障碍=相位器;
}
公开募捐{
试一试{
布尔stillLoop=true;int i=0;
while(静止循环){
整数大小;
Future=null;
试一试{
future=executorService.submit(新读卡器(this.barrier));//休眠
size=future.get();
}捕获(InterruptedException | ExecutionException ex){
//处理错误
}
如果(i==3){
stillLoop=false;
这个.障碍.前进(0);
this.isFinished=true;
}
//System.out.println(“i=“+i+”大小为:“+Size+”\r”);
i++;
}
}捕获(异常e1){
e1.printStackTrace();
}
}
公共布尔getIsFinished(){
返回此文件。已完成;
}
}
类读取器实现了可调用{