package com.day.twelve;
public class ThreadTst {
private int a;
public static void main(String args[]) {
ThreadTst ts = new ThreadTst();
InPut in = ts.new InPut();
OutPut out = ts.new OutPut();
Thread t = new Thread(in);//创建两个线程
Thread t1 = new Thread(out);
t.start();//启动线程
t1.start();
}
private synchronized void inPut() {
a++;
System.out.println("inPut = " + a);
}
private synchronized void outPut() {
a--;
System.out.println("outPut = " + a);
}
class InPut implements Runnable {
public void run() {//重写run()方法
for (int x = 0; x < 100;x++) {
inPut();
}
}
}
//OutPut实现Runnable接口
class OutPut implements Runnable {
public void run() {//重写run()方法
for (int x = 0; x < 100; x++) {
outPut();
}
}
}
}
|