【Java并发编程实战-阅读笔记】02-对象的共享
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Java并发编程实战-阅读笔记】02-对象的共享,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6769字,纯文字阅读大概需要10分钟。
内容图文
3.1 可见性
public class NoVisibility { private static boolean ready; private static int number; public static class ReaderThread extends Thread { publicvoid run() { while (!ready) { Thread.yield(); } System.out.println(number); } } publicstaticvoid main(String[] args) throws Exception { new ReaderThread().start(); number = 42; ready = true; System.out.println("赋值结束"); } }
public class NoVisibility { private static boolean ready; private static int number; public static class ReaderThread extends Thread { publicvoid run() { int i = 0; while (!ready) { i++; /* 把下面这句print代码放开,就能触发内存更新,线程才能读取新的ready的值。 */// System.out.println("--进入循环体-");/*通知系统放弃执行该线程,转交其他线程,自己可能会由运行态-->可运行态 */// Thread.yield(); } System.out.println(number + "," + i); } } publicstaticvoid main(String[] args) throws Exception { new ReaderThread().start(); Thread.sleep(5); // TimeUnit.MILLISECONDS.sleep(10); number = 42; ready = true; System.out.println("赋值结束"); } }
一、失效数据
@NotThreadSafe public class MutableInteger(){ private int value; public int get(){return value;} publicvoid set(int value){this.value = value;} }
@ThreadSafe public class MutableInteger(){ private int value; public synchronized int get(){return value;} publicsynchronizedvoid set(int value){this.value = value;} }
二、非原子的64位操作
三、加锁与可见性
四、Volatile变量
3.2 发布与溢出
public static Set<Secret> knownSecrets; publicvoid initialize(){ knownSecrets = new HashSet<Secret>(); }
class UnsafeStates { private String[] states = new String[]{"AK","AL"} public String[] getStates(){return states;} }
public class ThisEscape { public ThisEscape(EventSource source){ source.registerListener{ new EventListener(){ public void onEvent(Event e){ doSomething(); } } } } public void doSomething(){ // …… } }
public class ThisEscape { private String name; public ThisEscape(String name) { new Thread(new Runnable() { @Override publicvoid run() { System.out.println(ThisEscape.this.name); } }).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.name = name; } publicstaticvoid main(String[] args) { new ThisEscape("shenggang"); } }
public class SafeListener { private final EventListener listener; private SafeListener(){ listener = new EventListener() { publicvoid onEvent(Event e){ doSomething(e); } }; } publicstatic SafeListener newInstance(EventSource source){ SafeListener safe = new SafeListener(); source.registerListener(safe.listener); return safe; } }
3.3 线程封闭
(1)Ad-hoc线程封闭
(2)栈封闭
privateint loadTheArk(Collection<Animal> candidates) { SortedSet<Animal> animals; int numPairs = 0; Animal candidate = null; /* animals被封闭在方法中 */ animals = new TreeSet<Animal>(); animals.addAll(candidates); for(Animal a : animals){ if(candidate == null || !candidate.isGood()){ candidate = a; } else { ++numPairs; } } return numPairs; }
(3)ThreadLocal类
(4)不变性
原文:https://www.cnblogs.com/shenggang/p/8521911.html
内容总结
以上是互联网集市为您收集整理的【Java并发编程实战-阅读笔记】02-对象的共享全部内容,希望文章能够帮你解决【Java并发编程实战-阅读笔记】02-对象的共享所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。