1700451219
1700451220
static class Foo{
1700451221
1700451222
public synchronized void m1(){
1700451223
1700451224
try{
1700451225
1700451226
Thread.sleep(1000);
1700451227
1700451228
}catch(InterruptedException e){
1700451229
1700451230
//异常处理
1700451231
1700451232
}
1700451233
1700451234
System.out.println(“m1执行完毕”);
1700451235
1700451236
}
1700451237
1700451238
public synchronized void m2(){
1700451239
1700451240
System.out.println(“m2执行完毕”);
1700451241
1700451242
}}
1700451243
1700451244
public static void main(String[]args)throws Exception{
1700451245
1700451246
final Foo foo=new Foo();
1700451247
1700451248
//定义一个线程
1700451249
1700451250
Thread t=new Thread(new Runnable(){
1700451251
1700451252
public void run(){
1700451253
1700451254
foo.m1();
1700451255
1700451256
}
1700451257
1700451258
});
1700451259
1700451260
t.start();
1700451261
1700451262
//等待t1线程启动完毕
1700451263
1700451264
Thread.sleep(10);
1700451265
1700451266
//m2方法需要等待m1执行完毕
1700451267
1700451268
foo.m2();
[
上一页 ]
[ :1.700451219e+09 ]
[
下一页 ]