【发布时间】:2017-10-20 19:38:19
【问题描述】:
我编写了一个程序,运行一些线程,这些线程调用产生一些输出的方法。每个线程都会产生一些输出,如下所示:
a //output made by the thread 1
b //output made by the thread 2
c //output made by the thread 3
d //output made by the thread 2
e //output made by the thread 1
f //output made by the thread 3
我想用这种方式打印这个输出:
task1: a //output made by the thread 1
task2: b //output made by the thread 2
task3: c //output made by the thread 3
task2: d //output made by the thread 2
task1: e //output made by the thread 1
task3: f //output made by the thread 3
我需要在被称为 System.out.println 时附加 task#: 的东西。
run() 方法是这样的:
@Override
public void run() {
long start = 0, end = 0;
start = System.currentTimeMillis();
try {
myClass.do(param1, param2); //this is the method that produce the output
} catch (IOException ex) {
ex.printStackTrace();
}
end = System.currentTimeMillis();
System.out.println("Thread time: "+ (end - start));
}
这是run()中调用的方法:
@Override
public void do(String u, String p) throws IOException {
System.out.println("output1: "+ u);
System.out.println("output2: "+ p);
System.out.println("output3");
}
我希望在所有ouput1、ouput2、ouput3 之前显示task#:;你有什么想法吗?
我希望我的解释足够清楚。 提前致谢。
【问题讨论】:
-
你能分享一些代码吗?如果没有上下文,很难为您提供帮助。
-
@Mureinik 感谢您的评论,我更新了我的问题。
标签: java multithreading multitasking