Thread01:互斥
来源:互联网 发布:网络流行文化的研究 编辑:程序博客网 时间:2024/05/17 08:50
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(){ finalOutPut output=new OutPut();new Thread(new Runnable() {@Overridepublic void run() {while(true){try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}output.output("haizi");}}}).start(); new Thread(new Runnable() {@Overridepublic 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(){ finalOutPut output=new OutPut();new Thread(new Runnable() {@Overridepublic void run() {while(true){try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}output.output("haizi");}}}).start(); new Thread(new Runnable() {@Overridepublic 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(){ finalOutPut output=new OutPut();new Thread(new Runnable() {@Overridepublic void run() {while(true){try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}output.output("haizi");}}}).start(); new Thread(new Runnable() {@Overridepublic 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();}}}
0 0
- Thread01:互斥
- thread01
- 互斥
- 互斥
- 互斥
- 互斥
- hdu4135 互斥+容斥原理
- 进程互斥问题
- 文件读写互斥
- 多線程同步(互斥)
- 互斥概念
- 创建互斥对象
- 对象互斥
- 互斥对象(转)
- VC中的互斥
- 同步互斥
- 信号量互斥
- 同名进程互斥
- 使用Google Analytics跟踪捕获JavaScript,AngularJS,jQuery的在线错误和异常
- Servlet学习总结一
- 配置struts2不当错误
- activity切换动画总结
- 机房收费系统颗粒大归仓之----MSFlexGrid控件
- Thread01:互斥
- Maven 学习笔记(基本概念)
- 原生旧版信息流强切新版逻辑
- 欢迎使用CSDN-markdown编辑器
- 【DOM操作】实时监听input[type='text']和HTML标签中属性改变事件
- 对SVM的认识
- beetl模板入门例子
- 剑指offer刷题之c++实现的求链表中倒数第k个值
- IPC(进程间通信)机制