62,612
社区成员
发帖
与我相关
我的任务
分享
package icrement;
public class Counter {
protected long value = 0;
public long getValue() {
return value;
}
public long increase(){
synchronized (this) {
this.value += 10;
return value;
}
}
}
package icrement;
public class Thread1 extends Thread{
private volatile Counter counter ;
public Thread1(Counter counter){
this.counter = counter;
}
@Override
public void run(){
//synchronized (counter) {
//System.out.println(counter.value+=10);
//}
System.out.println(this.counter.increase());
}
}
package icrement;
public class Counter {
protected long value = 0;
public long getValue() {
return value;
}
public long increase(){
//synchronized (this) {
this.value += 10;
return value;
//}
}
}
package icrement;
public class Thread1 extends Thread{
private volatile Counter counter ;
public Thread1(Counter counter){
this.counter = counter;
}
@Override
public void run(){
synchronized (counter) {
//System.out.println(counter.value+=10);
System.out.println(this.counter.increase());
}
}
}