package com.dairui.chars;
public class ThreadTest {
public static void main(String[] args) {
ThreadDemo1 threadDemo1 = new ThreadDemo1();
ThreadDemo1.A a = threadDemo1.new A();
ThreadDemo1.B b1 = threadDemo1.new B();
Thread thread = null;
for (int i = 0; i < 2; i++) {
thread = new Thread(a);
thread.start();
thread = new Thread(b1);
thread.start();
}
}
}
class ThreadDemo1 {
static int i = 2;
private synchronized void add() {
// TODO Auto-generated method stub
i++;
System.out.println("线程" + Thread.currentThread().getName() + "A" + i);
}
private synchronized void reduce() {
// TODO Auto-generated method stub
i--;
System.out.println("线程" + Thread.currentThread().getName() + "B" + i);
}
public class A extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
add();
}
}
}
public class B extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
reduce();
}
}
}
}