/**设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。*/
public class Test
{
private int j;
public static void main(String args[])
{
Test tt=new Test();
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 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<3;i++)
{
inc();
}
}
}
class Dec implements Runnable
{
public void run()
{
for(int i=0;i<3;i++)
{
dec();
}
}
}
}
结果:
Thread-0-inc:1
Thread-0-inc:2
Thread-0-inc:3
Thread-2-inc:4
Thread-2-inc:5
Thread-2-inc:6
Thread-1-dec:5
Thread-1-dec:4
Thread-1-dec:3
Thread-3-dec:2
Thread-3-dec:1
Thread-3-dec:0