我必须同时运行两个线程,其中一个输出数字1到500,而另一个打印编号501到1000。输出必须显示并发性。
发布于 2016-06-29 10:58:21
试试下面的代码。主线程打印数字从1到500,其他线程打印从那里到1000。
public class Test implements Runnable{
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Test());
t.start();
for (int i = 1 ; i <=500 ; i++)
System.out.println("Main Thread printing "+i);
}
@Override
public void run() {
for (int i=501; i <=1000 ; i++)
System.out.println("Thread 1 printing "+i);
}
}https://stackoverflow.com/questions/38097379
复制相似问题