设计4个线程,其中两个线程每次对j增加1,另外两个线程每次对对j每次减少1,请编写程序
package www.tread.com;
public class Test070 {
private static int j = 0;
public static void main(String[] args) {
Test070 test = new Test070();
A a = test.new A();
B b = test.new B();
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(a);
thread.start();
Thread thread1 = new Thread(b);
thread1.start();
}
}
public void a() {
j++;
System.out.println(Thread.currentThread().getName() + "j=" + j);
}
public void b() {
j--;
System.out.println(Thread.currentThread().getName() + "j=" + j);
}
class A implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++)
a();
}
}
class B implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++)
b();
}
}
}