package threadtest1;public class threadtest1 {
private int j;
Thread thread = new Thread();
private synchronized void inc() {
j++;
System.out.println(thread.currentThread().getName() + "-inc:" + j);
} private synchronized void dec() {
j--;
System.out.println(thread.currentThread().getName() + "-dec:" + j);
} class inc implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
inc();
}
}
}
class dec implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
dec();
}
}
} public static void main(String[] args) {
threadtest1 tt = new threadtest1();
inc inc = tt.new inc();
dec dec = tt.new dec();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.start();
t = new Thread(dec);
t.start();
} }
}
private int j;
Thread thread = new Thread();
private synchronized void inc() {
j++;
System.out.println(thread.currentThread().getName() + "-inc:" + j);
} private synchronized void dec() {
j--;
System.out.println(thread.currentThread().getName() + "-dec:" + j);
} class inc implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
inc();
}
}
}
class dec implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
dec();
}
}
} public static void main(String[] args) {
threadtest1 tt = new threadtest1();
inc inc = tt.new inc();
dec dec = tt.new dec();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.start();
t = new Thread(dec);
t.start();
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货