【发布时间】:2014-11-15 12:21:39
【问题描述】:
它想要这样的序列:3 7 13 17 23 27 33 37 ....up to 100
但我得到以下输出:
3 13 23 33..
7 17 27 37
代码是:
class abc extends Thread
{
public void run()
{
int i;
for(i=3; i<97; i+=10)
{
System.out.println(i);
try
{ sleep(100);}
catch(Exception e){}
}
for(i=7; i<97; i+=10)
{
System.out.println(i);
try
{ sleep(100);}
catch(Exception e){}
}
}
}
class Print3n7n13n17
{
public static void main(String args[])
{
abc p= new abc();
abc p1= new abc();
p.start();
p1.start();
}
}
【问题讨论】:
-
这就是线程的全部意义所在。它们同时执行。如果你想打印一个序列,你应该使用顺序代码。
-
序列是
+4 +6 +4 +6,您正在添加+10 +10 +10。为什么你的代码不起作用并不奇怪? -
思考如何解决问题是绝对错误的。我认为你应该了解一下多线程是如何工作的。这个问题甚至不需要多线程
标签: java multithreading for-loop