关于线程同步的问题:
例如下面的程序代码:
两个方法被锁定了,为什么执行的时候是等m1 执行完了m2才能开始执行啊?? 而当m2没有被锁定的时候就不是呢??
public class TT implements Runnable
{
int b=100; public synchronized void m1() throws Exception
{
b=1000;
Thread.sleep(5000);
System.out.println("b=" + b);
} public synchronized void m2()
{
System.out.println(b);
} public void run()
{
try
{
m1();
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void main(String[] args) throws Exception
{
TT tt=new TT();
Thread t=new Thread(tt);
t.start(); Thread.sleep(1000);
tt.m2();
}
} 请指点指点...初学者
例如下面的程序代码:
两个方法被锁定了,为什么执行的时候是等m1 执行完了m2才能开始执行啊?? 而当m2没有被锁定的时候就不是呢??
public class TT implements Runnable
{
int b=100; public synchronized void m1() throws Exception
{
b=1000;
Thread.sleep(5000);
System.out.println("b=" + b);
} public synchronized void m2()
{
System.out.println(b);
} public void run()
{
try
{
m1();
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void main(String[] args) throws Exception
{
TT tt=new TT();
Thread t=new Thread(tt);
t.start(); Thread.sleep(1000);
tt.m2();
}
} 请指点指点...初学者
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货