您的位置:首页 > 其它

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();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: