年人力资源知识的作用

上传人:精****库 文档编号:136405904 上传时间:2020-06-28 格式:DOC 页数:7 大小:44.11KB
返回 下载 相关 举报
年人力资源知识的作用_第1页
第1页 / 共7页
年人力资源知识的作用_第2页
第2页 / 共7页
年人力资源知识的作用_第3页
第3页 / 共7页
年人力资源知识的作用_第4页
第4页 / 共7页
年人力资源知识的作用_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《年人力资源知识的作用》由会员分享,可在线阅读,更多相关《年人力资源知识的作用(7页珍藏版)》请在金锄头文库上搜索。

1、 synchronized的作用一、同步方法public synchronized void methodAAA()/.锁定的是调用这个同步方法的对象测试:a、不使用这个关键字修饰方法,两个线程调用同一个对象的这个方法。目标类:1public class TestThread 2 public void execute() /synchronized,未修饰3 for(int i=0;i100;i+)4 System.out.println(i);5 6 7线程类:1public class ThreadA implements Runnable2 TestThread test=null;3

2、 public ThreadA(TestThread pTest) /对象有外部引入,这样保证是同一个对象4 test=pTest;5 6 public void run() 7 test.execute();8 9调用:1TestThread test=new TestThread();2Runnable runabble=new ThreadA(test);3Thread a=new Thread(runabble,A); 4a.start();5Thread b=new Thread(runabble,B);6b.start();结果:输出的数字交错在一起。说明不是同步的,两个方法在不同

3、的线程中是异步调用的。b、修改目标类,增加synchronized修饰1public class TestThread 2 public synchronized void execute() /synchronized修饰3 for(int i=0;i100;i+)4 System.out.println(i);5 6 7结果:输出的数字是有序的,首先输出A的数字,然后是B,说明是同步的,虽然是不同的线程,但两个方法是同步调用的。注意:上面虽然是两个不同的线程,但是是同一个实例对象。下面使用不同的实例对象进行测试。c、每个线程都有独立的TestThread对象。目标类:1public cla

4、ss TestThread 2 public synchronized void execute() /synchronized修饰3 for(int i=0;i100;i+)4 System.out.println(i);5 6 7线程类:1public class ThreadA implements Runnable2 public void run() 3 TestThread test=new TestThread();4 test.execute();5 67调用:1Runnable runabble=new ThreadA();2Thread a=new Thread(runab

5、ble,A); 3a.start();4Thread b=new Thread(runabble,B);5b.start();结果:输出的数字交错在一起。说明虽然增加了synchronized 关键字来修饰方法,但是不同的线程调用各自的对象实例,两个方法仍然是异步的。引申:对于这种多个实例,要想实现同步即输出的数字是有序并且按线程先后顺序输出,我们可以增加一个静态变量,对它进行加锁(后面将说明锁定的对象)。修改目标类:1public class TestThread 2 private static Object lock=new Object(); /必须是静态的。3 public void

6、 execute()4 synchronized(lock)5 for(int i=0;i100;i+)6 System.out.println(i);7 8 9 10二、同步代码块1public void method(SomeObject so)2 synchronized(so)3 /.4 5锁定一个对象,其实锁定的是该对象的引用(object reference)谁拿到这个锁谁就可以运行它所控制的那段代码。当有一个明确的对象作为锁时,就可以按上面的代码写程序,但当没有明确的对象作为锁,只是想让一段代码同步时,可以创建一个特殊的instance变量(它必须是一个对象)来充当锁(上面的解决

7、方法就是增加了一个状态锁)。a、锁定一个对象,它不是静态的private byte lock = new byte0; / 特殊的instance变量目标类:1public class TestThread 2 private Object lock=new Object(); 3 public void execute()4 synchronized(lock) /增加了个锁,锁定了对象lock,在同一个类实例中,是线程安全的,但不同的实例还是不安全的。56因为不同的实例有不同对象锁lock7 for(int i=0;i100;i+)8 System.out.println(i);9 10

8、11 12 其实上面锁定一个方法,等同于下面的:1public void execute() 2 synchronized(this) /同步的是当然对象3 for(int i=0;i100;i+)4 System.out.println(i);5 6 7b、锁定一个对象或方法,它是静态的这样锁定,它锁定的是对象所属的类public synchronized static void execute() /.等同于1public class TestThread 2 public static void execute()3 synchronized(TestThread.class)4 /5

9、6 7测试:目标类:1public class TestThread 2 private static Object lock=new Object();3 public synchronized static void execute() /同步静态方法4 for(int i=0;i100;i+)5 System.out.println(i);6 7 8 public static void execute1()9 for(int i=0;i100;i+)10 System.out.println(i);11 12 13 public void test()14 execute(); /输出

10、是有序的,说明是同步的15 /execute1(); /输出是无须的,说明是异步的16 17线程类:调用不同的方法,于是建立了两个线程类1public class ThreadA implements Runnable2 public void run() 3 TestThread.execute();/调用同步静态方法4 56public class ThreadB implements Runnable7 public void run() 8 TestThread test=new TestThread();9 test.test();/调用非同步非静态方法10 11调用:1Runnab

11、le runabbleA=new ThreadA();2Thread a=new Thread(runabbleA,A); 3a.start();4Runnable runabbleB=new ThreadB();5Thread b=new Thread(runabbleB,B); 6b.start();注意:1、用synchronized 来锁定一个对象的时候,如果这个对象在锁定代码段中被修改了,则这个锁也就消失了。看下面的实例:目标类:1public class TestThread 2 private static final class TestThreadHolder 3 priva

12、te static TestThread theSingleton = new TestThread();4 public static TestThread getSingleton() 5 return theSingleton;6 7 private TestThreadHolder() 8 9 10 11 private Vector ve =null;12 private Object lock=new Object();13 private TestThread()14 ve=new Vector();15 initialize();16 17 public static TestThread getInstance()18

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 企业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号