先读一读这样一段程序:
public class Example
{
public static void main(String args[])
{
Lefthand left;
Righthand right;
left=new Lefthand();
right=new Righthand();
left.start();
right.start();
}
}class Lefthand extends Thread
{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.print("A");
try
{
sleep(500);
}
catch(InterruptedException e)
{
}
}
}
}class Righthand extends Thread
{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.print("B");
try
{
sleep(300);
}
catch(InterruptedException e)
{
}
}
}
}上面程序的打印结果是ABBABBABAA现在的问题是当程序经过了1500毫秒的时候为什么打印结果是两个A,也就是上述打印结果末尾的两个A?
public class Example
{
public static void main(String args[])
{
Lefthand left;
Righthand right;
left=new Lefthand();
right=new Righthand();
left.start();
right.start();
}
}class Lefthand extends Thread
{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.print("A");
try
{
sleep(500);
}
catch(InterruptedException e)
{
}
}
}
}class Righthand extends Thread
{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.print("B");
try
{
sleep(300);
}
catch(InterruptedException e)
{
}
}
}
}上面程序的打印结果是ABBABBABAA现在的问题是当程序经过了1500毫秒的时候为什么打印结果是两个A,也就是上述打印结果末尾的两个A?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货