题目:使用多线程,打印1~1000以内的数,每打印10个偶数,就打印10个奇数输出结果只有:
F:\>java TTest
偶数:0
偶数:2
偶数:4
偶数:6
偶数:8
偶数:10
偶数:12
偶数:14
偶数:16
偶数:18
奇数:1
奇数:3
奇数:5
奇数:7
奇数:9
奇数:11
奇数:13
奇数:15
奇数:17
奇数:19public class TTest{ /**
* @param args
*/
static int i=0;
static int j=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
TTest t=new TTest();
aa a=new aa(t);
bb b=new bb(t);
Thread t1=new Thread(a);//线程实例化
Thread t2=new Thread(b);
t1.start();
t2.start();
}
public synchronized void getEven()
{
int num=0;
for(;i<=1000;i++)
{
while(num==10)
{
try
{
this.wait();//等待
}
catch(InterruptedException e)
{}
}
if((i&1)==0)
{
System.out.println("偶数:"+i);
num++;
}
}
this.notify();
}
public synchronized void getOdd()
{
int num=0;
for(;j<=1000;j++)
{
while(num==10)
{
try
{
this.wait();//等待,
}
catch(InterruptedException e)
{}
}
if((j&1)==1)
{
System.out.println("奇数:"+j);
num++;
}
}
this.notify();
}
}
class aa implements Runnable
{
TTest thetest;
public aa(TTest s)
{
this.thetest=s;
}
public void run()
{
thetest.getEven();
}
}
class bb implements Runnable
{
TTest thetest;
public bb(TTest s)
{
this.thetest=s;
}
public void run()
{
thetest.getOdd();
}
}
F:\>java TTest
偶数:0
偶数:2
偶数:4
偶数:6
偶数:8
偶数:10
偶数:12
偶数:14
偶数:16
偶数:18
奇数:1
奇数:3
奇数:5
奇数:7
奇数:9
奇数:11
奇数:13
奇数:15
奇数:17
奇数:19public class TTest{ /**
* @param args
*/
static int i=0;
static int j=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
TTest t=new TTest();
aa a=new aa(t);
bb b=new bb(t);
Thread t1=new Thread(a);//线程实例化
Thread t2=new Thread(b);
t1.start();
t2.start();
}
public synchronized void getEven()
{
int num=0;
for(;i<=1000;i++)
{
while(num==10)
{
try
{
this.wait();//等待
}
catch(InterruptedException e)
{}
}
if((i&1)==0)
{
System.out.println("偶数:"+i);
num++;
}
}
this.notify();
}
public synchronized void getOdd()
{
int num=0;
for(;j<=1000;j++)
{
while(num==10)
{
try
{
this.wait();//等待,
}
catch(InterruptedException e)
{}
}
if((j&1)==1)
{
System.out.println("奇数:"+j);
num++;
}
}
this.notify();
}
}
class aa implements Runnable
{
TTest thetest;
public aa(TTest s)
{
this.thetest=s;
}
public void run()
{
thetest.getEven();
}
}
class bb implements Runnable
{
TTest thetest;
public bb(TTest s)
{
this.thetest=s;
}
public void run()
{
thetest.getOdd();
}
}
你看下修正过的代码://注释见getOdd()方法
public synchronized void getEven()
{
int num=0;
for(;i <=1000;i++)
{
if(num==10)
{
num = 0;
try
{
this.wait();//等待
}
catch(InterruptedException e)
{}
}
if((i&1)==0)
{
System.out.println("偶数:"+i);
num++;
}
this.notify();
}
}
public synchronized void getOdd()
{
int num=0;
for(;j <1000;j++)//这里要小于,不能是小于等于,要不然的话打印完毕后程序就等在哪了,不会结束
{
if(num==10)//这里用if不用while
{
num = 0;//当num=10时把它设置回0,重新计数
try
{
this.wait();//等待
}
catch(InterruptedException e)
{}
}
if((j&1)==1)
{
System.out.println("奇数:"+j);
num++;
}
this.notify();//notify()放到for循环里面
}
}
}
public class OddEvenNum {
//打基数
public synchronized void printOdd() {
int count = 0;
for (int i = 1; i < 1000; i = i + 2) {
System.out.println("基数:"+i);
count++;
if (count % 10 == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
}
}
//打偶数
public synchronized void printEven() {
int count = 0;
for (int i = 0; i <= 1000; i = i + 2) {
System.out.println("偶数:"+i);
count++;
if (count % 10 == 0) {
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace();
}
}
this.notify();
}
} public static void main(String[] args) {
OddEvenNum param = new OddEvenNum();
Odd odd = new Odd(param);
Even even = new Even(param);
Thread oddThread = new Thread(odd);
Thread evenThread = new Thread(even);
evenThread.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
oddThread.start();
}}class Odd implements Runnable {
private OddEvenNum param; public Odd(OddEvenNum param) {
this.param = param;
} public void run() {
param.printOdd();
}}class Even implements Runnable {
private OddEvenNum param; public Even(OddEvenNum param) {
this.param = param;
} public void run() {
param.printEven();
}}
{
int num=0;
for(;j <1000;j++)//这里要小于,不能是小于等于,要不然的话打印完毕后程序就等在哪了,不会结束(在哪了看是否结束啊!好像没关系吧 )
{
if(num==10)//这里用if不用while (为什么啊?我用while也能得到这个结果)
{
num = 0;//当num=10时把它设置回0,重新计数
try
{
this.wait();//等待
}
catch(InterruptedException e)
{}
}
if((j&1)==1)
{
System.out.println("奇数:"+j);
num++;
}
this.notify();//notify()放到for循环里面
}