-5 java multithreading for-loop
它希望序列像:3 7 13 17 23 27 33 37 ....up to 100
但我得到以下输出:
3 13 23 33..
7 17 27 37
Run Code Online (Sandbox Code Playgroud)
代码是:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
既然没有什么是你的问题说你必须使用两个线程,为什么不简单?
for(i=3; i<97; i+=10)
{
System.out.println(i);
System.out.println(i+4);
}
Run Code Online (Sandbox Code Playgroud)