我觉得这个题应该考到volatile变量,虽然不大常用,借用了 tangweiwei_csdn() 的架子,不好意思,以下在Jbuilder9下通过:
public class Dif {
volatile int hehe=0;
Thread t1=new ThreadAdd1();
Thread t2 = new ThreadAdd2();
Thread t3 = new ThreadSub3();
Thread t4 = new ThreadSub4(); class ThreadAdd1 extends Thread
{
ThreadAdd1()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe++;
System.out.println("t1 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadAdd2 extends Thread
{
ThreadAdd2()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe++;
System.out.println("t2 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadSub3 extends Thread
{
ThreadSub3()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe--;
System.out.println("t3 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadSub4 extends Thread
{
ThreadSub4()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe--;
System.out.println("t4 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
public Dif() {
t1.start();
t2.start();
t3.start();
t4.start();
} public static void main(String arg[]) {
Dif dif=new Dif();
}
}输出结果:(一种)
t1 Thread Run,Var=1t2 Thread Run,Var=2t3 Thread Run,Var=1t1 Thread Run,Var=2t2 Thread Run,Var=3t3 Thread Run,Var=2t4 Thread Run,Var=1t1 Thread Run,Var=2
t1 Thread Run,Var=2t2 Thread Run,Var=3t3 Thread Run,Var=2t4 Thread Run,Var=1t4 Thread Run,Var=0
public class Dif {
volatile int hehe=0;
Thread t1=new ThreadAdd1();
Thread t2 = new ThreadAdd2();
Thread t3 = new ThreadSub3();
Thread t4 = new ThreadSub4(); class ThreadAdd1 extends Thread
{
ThreadAdd1()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe++;
System.out.println("t1 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadAdd2 extends Thread
{
ThreadAdd2()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe++;
System.out.println("t2 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadSub3 extends Thread
{
ThreadSub3()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe--;
System.out.println("t3 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
class ThreadSub4 extends Thread
{
ThreadSub4()
{
//this.start();;
}
public void run()
{
for(int i=0;i<10;i++)
{
hehe--;
System.out.println("t4 Thread Run,Var="+hehe);
try
{
sleep((int)Math.random()*1000);
}catch(Exception E){}
}
}
}
public Dif() {
t1.start();
t2.start();
t3.start();
t4.start();
} public static void main(String arg[]) {
Dif dif=new Dif();
}
}输出结果:(一种)
t1 Thread Run,Var=1t2 Thread Run,Var=2t3 Thread Run,Var=1t1 Thread Run,Var=2t2 Thread Run,Var=3t3 Thread Run,Var=2t4 Thread Run,Var=1t1 Thread Run,Var=2
t1 Thread Run,Var=2t2 Thread Run,Var=3t3 Thread Run,Var=2t4 Thread Run,Var=1t4 Thread Run,Var=0
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货