public class Array {
int[]arr=new int[6];
public int c=0;
public void add( int num){
arr[c]=num;
c++;
}
}
public class ThreadDome1 implements Runnable {
private Array a;
public ThreadDome1(Array a) {
this.a=a;
}
@Override
public void run() {
if(a.c%2==0){
try {
a.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
a.add(a.c);
a.notify();
}
}
public class ThreadDome2 implements Runnable {
private Array a;
public ThreadDome2(Array a) {
this.a=a;
}
@Override
public void run() {
// TODO Auto-generated method stub
if((a.c%2)!=0){
try {
a.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
a.add(a.c);
a.notify();
}
}
public class Dome {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Array a=new Array();
ThreadDome1 td1=new ThreadDome1(a);
ThreadDome2 td2=new ThreadDome2(a);
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t2.start();
}
}
|
|