打字猴:1.700451203e+09
1700451203
1700451204 System.out.print(”*”);
1700451205
1700451206 }
1700451207
1700451208 System.out.println(i);
1700451209
1700451210 fun(i);
1700451211
1700451212 }
1700451213
1700451214 }
1700451215
1700451216 }
1700451217
1700451218 注意fun方法是一个递归函数,而且还加上了synchronized关键字,它保证同时只有一个线程能够执行,想想synchronized关键字的作用:当一个带有synchronized关键字的方法在执行时,其他synchronized方法会被阻塞,因为线程持有该对象的锁。比如有这样的代码:
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(){
[ 上一页 ]  [ :1.700451203e+09 ]  [ 下一页 ]