标签:public stat runnable err new t rgs har nts ide
public class ThreadDemo4 {
static ShareData data=new ShareData();
public static void main(String[] args) {
new Thread(new Myrunnable1(data)).start();
new Thread(new Myrunnable2(data)).start();
}
}
class Myrunnable1 implements Runnable{
int i=50;
private ShareData data;
public Myrunnable1(ShareData data){
this.data=data;
}
@Override
public void run() {
while(i>0){
i--;
data.increment();}
}
}
class Myrunnable2 implements Runnable{
int i=50;
private ShareData data;
public Myrunnable2(ShareData data){
this.data=data;
}
@Override
public void run() {
while(i>0){
i--;
data.decrement();}
}
}
class ShareData{
private int j=1;
public void increment(){
j++;
System.out.println("加一得 "+j);
}
public void decrement(){
j--;
System.out.println("减一得 "+j);
}
}
标签:public stat runnable err new t rgs har nts ide
原文地址:http://www.cnblogs.com/yugeng/p/7857697.html