您的位置:首页 > 编程语言 > Java开发

JavaSE(20)(多线程安全)

2015-12-02 15:09 246 查看
Father类:
package zz.itheima.multi_thread_security;

//嵌套锁是为了让死锁出现的概率大大增加
public class Father extends Thread{

@Override
public void run() {
synchronized (MyLock.obj1) {//交叉
System.out.println("爸爸有零花钱");
try {
MyLock.obj1.wait(2000);//及时的手工释放锁
} catch (Exception e) {
e.printStackTrace();
}
synchronized (MyLock.obj2) {//交叉
System.out.println("想要儿子的成绩单");

}
}
}

}
Son类:
package zz.itheima.multi_thread_security;

public class Son extends Thread{
//嵌套锁是为了让死锁出现的概率大大增加

@Override
public void run() {
synchronized (MyLock.obj2) {//交叉
System.out.println("儿子有成绩单");
try {
MyLock.obj2.wait(1000);//及时的手工释放锁
} catch (Exception e) {
e.printStackTrace();
}
synchronized (MyLock.obj1) {//锁里套锁
System.out.println("想要零花钱");
}
}
}

}
MyLock类:
package zz.itheima.multi_thread_security;

public class MyLock {
//有两个共享资源(零花钱和成绩单),所以需要两把锁
public static Object obj1 = new Object();
public static Object obj2 = new Object();
}
TestFatherSon类:
package zz.itheima.multi_thread_security;

public class TestFatherSon {

public static void main(String[] args) {
Father f = new Father();
Son s = new Son();

f.start();
s.start();
}

}
Kuang类:
package zz.itheima.multi_thread_security;

import java.util.ArrayList;
import java.util.List;

public class Kuang {
//水果筐:假设不能超过30个  静态是保证农夫和小孩用的是同一个筐
public static List<Integer> kuang = new ArrayList<>();
}
Chlidren类:
package zz.itheima.multi_thread_security;

public class Chlidren extends Thread{//消费者进程

@Override
public void run() {
while (true) {
synchronized (Kuang.kuang) {//实现线程通讯的中间人
if (Kuang.kuang.size()==0) {//如果狂为空的话,就不能吃了
try {
Kuang.kuang.wait();
} catch (Exception e) {
e.printStackTrace();
}
}else {
if (Kuang.kuang.size()>0) {
Kuang.kuang.remove(0);//一次吃一个
Kuang.kuang.notify();//通知农夫可以继续放水果了
System.out.println("吃了一个,目前还有"+Kuang.kuang.size()+"个");
try {
Thread.sleep(1);//模拟吃的速度
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

}
}

}
NongFu类:
package zz.itheima.multi_thread_security;

public class NongFu extends Thread{//生产者进程

@Override
public void run() {
while (true) {
synchronized (Kuang.kuang) {//实现线程间通讯的中间人
if (Kuang.kuang.size()==30) {//狂满了就不能添加了
try {
Kuang.kuang.wait();
} catch (Exception e) {
e.printStackTrace();
}
}else {
Kuang.kuang.add(1);//每次添加一个水果
Kuang.kuang.notify();//通知小孩可以吃了
System.out.println("添加了一个,目前 有"+Kuang.kuang.size()+"个");
try {
Thread.sleep(100);//模拟生产速度
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
}

}
TestNongFuChildren类:
package zz.itheima.multi_thread_security;

public class TestNongFuChildren {

public static void main(String[] args) {
NongFu n = new NongFu();
Chlidren c = new Chlidren();

n.start();
c.start();
}

}
Num1类:
package zz.itheima.multi_thread_security;

public class Num1 extends Thread{
@Override
public void run() {
while(true){
synchronized (Kuang.kuang) {
System.out.println(1);
Kuang.kuang.notify();
try {
Kuang.kuang.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}
Num2类:
package zz.itheima.multi_thread_security;

public class Num2 extends Thread{
@Override
public void run() {
while(true){
synchronized (Kuang.kuang) {
System.out.println(2);
Kuang.kuang.notify();
try {
Kuang.kuang.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}
TestNum类:
package zz.itheima.multi_thread_security;

public class TestNum {

public static void main(String[] args) {
new Num1().start();
new Num2().start();
}

}
SingleStudent类(单例设计模式):
package zz.itheima.multi_thread_security;
//懒汉式
public class SingleStudent {
private static SingleStudent ss = null;
private SingleStudent(){//private就保证了外部不能new

}
public synchronized static SingleStudent getInstance(){
if (ss==null) {
ss = new SingleStudent();
}
return ss;
}
}
TestSingleStudent类:
package zz.itheima.multi_thread_security;

public class TestSingleStudent {

public static void main(String[] args) {
SingleStudent ss1 = SingleStudent.getInstance();
SingleStudent ss2 = SingleStudent.getInstance();
System.out.println(ss1);
System.out.println(ss2);
}

}
运行结果:
zz.itheima.multi_thread_security.SingleStudent@649e4dc0
zz.itheima.multi_thread_security.SingleStudent@649e4dc0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: