Thread.sleep((long) Math.random() * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("World: " + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this.example = example; } @Override public void run() { example.execute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this.example = example; } @Override public void run() { example.execute2(); } } 如不雅去掉落synchronized关键字,则两个办法并发履行,并没有互相竽暌拱响。
然则如例子法度榜样中所写,即就是两个办法:
- public class ThreadTest
- {
- public static void main(String[] args)
- {
- Example example = new Example();
-
- Thread t1 = new Thread1(example);
- Thread t2 = new Thread2(example);
-
- t1.start();
- t2.start();
- }
-
推荐阅读
PHP与Golang如何通信?
惯例的筹划: 用Golang写一个http/TCP办事,php经由过程http/TCP与Golang通信 将Golang经由较多封装,做为php扩大。 PHP经由过程体系敕令,调取Golang的可履行文件存在的问题: >>>详细阅读
本文标题:Java多线程之synchronized关键字详解
地址:http://www.17bianji.com/lsqh/35484.html
1/2 1