public class 两个线程访问一个值 { public static void main(String[] args) { 值类 一个对象 = new 值类(); 线程A a = new 线程A(一个对象); 线程C c = new 线程C(一个对象); new Thread(a).start(); new Thread(c).start(); } } class 值类 { private int i; public int getI() { return i; } public void setI(int i) { this.i = i; }} class 线程A implements Runnable { private 值类 b; public 线程A(值类 a) { this.b = a; } public void run() { while(true) { System.out.println("b"+b.getI()); b.setI(b.getI()-1); }
}
} class 线程C implements Runnable { private 值类 c; public 线程C(值类 a) { this.c = a; } public void run() { while(true) {
public class 两个线程访问一个值 {
public static void main(String[] args) {
值类 一个对象 = new 值类();
线程A a = new 线程A(一个对象);
线程C c = new 线程C(一个对象);
new Thread(a).start();
new Thread(c).start();
}
}
class 值类
{
private int i; public int getI() {
return i;
} public void setI(int i) {
this.i = i;
}}
class 线程A implements Runnable
{
private 值类 b;
public 线程A(值类 a)
{
this.b = a;
}
public void run() {
while(true)
{
System.out.println("b"+b.getI());
b.setI(b.getI()-1);
}
}
}
class 线程C implements Runnable
{
private 值类 c;
public 线程C(值类 a)
{
this.c = a;
}
public void run() {
while(true)
{
System.out.println("c"+c.getI());
c.setI(c.getI()+1);
}
}
}