Thread01:互斥
2015-08-08 12:12
447 查看
package com.test; /** * * @author * 1、内部类不能访问外部非final对象 * 2、内部类可以直接访问外部类成员变量,即此时向内部类传入外部类实例 * 3、本类中的两个线程访问同一个output变量,此时没有做任何互斥措施, * 可能出现该对象输出的结果出现两个线程抢夺资源的情况, * 即可能会输出:babahaizi、hbaaiba等情况 * */ public class Test1 { public static void main(String[] args) { new Test1().init(); } public void init(){ final OutPut output=new OutPut(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("haizi"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("baba"); } } }).start(); } /** * * @author * 内部类,输出功能 */ class OutPut{ public void output(String name){ int len =name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
package com.test; /** * * @author * 1、内部类不能访问外部非final对象 * 2、内部类可以直接访问外部类成员变量,即此时向内部类传入外部类实例 * 3、该类在Test1基础上更改,对多线程访问的资源进行同步操作, * 并且互斥加在output方法中name变量上 * 4、只有对多个线程访问的统一资源进行互斥synchronized,才能避免线程之间抢夺资源 */ public class Test2 { public static void main(String[] args) { new Test2().init(); } public void init(){ final OutPut output=new OutPut(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("haizi"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("baba"); } } }).start(); } /** * * @author * 内部类,输出功能 * 1、synchronized:作用在变量上 * 2、作用在方法上 */ class OutPut{ /** * 作用在变量上,此时同步的是name * @param name */ public void output(String name){ int len =name.length(); //在name变量上加上同步操作,防止线程抢夺资源,当某一线程获得该资源后, //只有该线程使用完资源之后,其他线程才能使用。 synchronized (name) { for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } /** * 作用在方法上,此时同步的是当前对象this * @param name */ public synchronized void output2(String name){ int len =name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } /** * 作用在方法上,此时同步的是当前对象this * @param name */ public void output3(String name){ synchronized (this) { int len =name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } } }
package com.test; /** * * @author * 1、内部类不能访问外部非final对象 * 2、内部类可以直接访问外部类成员变量,即此时向内部类传入外部类实例 * */ public class Test3 { public static void main(String[] args) { new Test3().init(); } public void init(){ final OutPut output=new OutPut(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("haizi"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } output.output("baba"); } } }).start(); } /** * * @author * 内部类,输出功能 * OutPut.class:类的字节码在内存中也是一份变量, * 所以以下两个方法互斥,共享同一对象 */ static class OutPut{ public void output(String name){ synchronized (OutPut.class) { int len =name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } /** * 静态方法同步 * @param name */ public static synchronized void output3(String name){ int len =name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
相关文章推荐
- 【DOM操作】实时监听input[type='text']和HTML标签中属性改变事件
- Maven 学习笔记(基本概念)
- Defining a controller with @Controller
- AngularJs学习笔记__4、自定义指令
- 设计-简约而不简单
- ASP.NET中Cookie的用法实例分析
- linux上传下载(scp篇)
- [笔记][Java7并发编程实战手册]2.2使用syncronized实现同步方法
- 配置struts2不当错误
- FZU 2196 Escape(BFS预处理+BFS搜索)
- 机房收费系统颗粒大归仓之----MSFlexGrid控件
- iOS UIView控件的常用属性和方法的总结
- ADT SVN 插件安装教程
- activity切换动画总结
- 使用Google Analytics跟踪捕获JavaScript,AngularJS,jQuery的在线错误和异常
- 简单的Linux 驱动模块编译,加载过程
- 如何编译一个linux下的驱动模块(1)
- hibernate延迟加载(get和load的区别)
- Errors occurred during the build. Errors running builder 'JavaScript Validator' on project
- Windows下Redis的安装使用