#Java编程# 编写一个应用程序创建两个线程,一个线程打印输出1~100之间所有的奇数,另外一
展开全部
import java.util.Random;
class A extends Thread
{
int i=1;
Random r=new Random();
public void run()
{
while(i<100)
{
System.out.println("奇数:"+i);
i+=2;
try
{
Thread.sleep(r.nextInt(500));
}
catch(InterruptedException e)
{
e.printStackTrace();
};
}
}
}
class B implements Runnable
{
int i=2;
Random r=new Random();
public void run()
{
while(i<=100)
{
System.out.println("偶数:"+i);
i+=2;
try
{
Thread.sleep(r.nextInt(500));
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public class TestThread
{
public static void main(String[] args)
{
new A().start();
new Thread(new B()).start();
}
}
class A extends Thread
{
int i=1;
Random r=new Random();
public void run()
{
while(i<100)
{
System.out.println("奇数:"+i);
i+=2;
try
{
Thread.sleep(r.nextInt(500));
}
catch(InterruptedException e)
{
e.printStackTrace();
};
}
}
}
class B implements Runnable
{
int i=2;
Random r=new Random();
public void run()
{
while(i<=100)
{
System.out.println("偶数:"+i);
i+=2;
try
{
Thread.sleep(r.nextInt(500));
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public class TestThread
{
public static void main(String[] args)
{
new A().start();
new Thread(new B()).start();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询