java多线程嵌套_Java多线程进阶
需要理解鎖是什么意思?
首先是發生在多線程的情況下;
線程a,線程b,共享資源share
例如,share的資源一次只能被一個對象操作,這時候需要一個東西來標識(也叫監視器)出來表明該資源已經有人(指的是線程)在使用了,請還要使用的人(指的是線程)進入等待(線程等待池/隊列);
怎么實現呢?
synchronized關鍵字,配合一個唯一的東西,可以是類.class,也可以是對象本身;但注意的是必須是一個引用的數據類型,如包裝類,obj等
1.生產者和消費者模式
代碼演示:
重點練習1:
一圓桌前坐著5位哲學家,兩個人中間有一只筷子,桌子中央有面條。哲學家思考問題,當餓了的時候拿起左右兩只筷子吃飯,必須拿到兩只筷子才能吃飯.那思考必須 把兩只筷子放回才可以思考.
重點練習2:
桌子上有一個空盤,允許存放1個水果.爸爸想其中放蘋果也可以放橘子.兒子專門等吃橘子.女兒專門等吃蘋果!
publicclassDemo {
classFriutPlate {
String name;
publicsynchronizedvoidaddFriut(String name) throwsInterruptedException {
while(this.name!= null) {
System.out.println("盤子里已經有"+ this.name+ "生產者暫停");
this.wait();
}
this.notifyAll();
this.name= name;
System.out.println("向水果盤中放入:"+ name);
Thread.sleep(100);
}
publicsynchronizedString getFriut() throwsInterruptedException {
while(this.name== null) {
System.out.println("盤子無水果,消費者暫停");
this.wait();
}
this.notifyAll();
System.out.println("盤子中的:"+ name+ "消費者準備消費");
Thread.sleep(100);
returnname;
}
publicvoidsetNameNull() {
name= null;
}
}
classProductApple implementsRunnable {
FriutPlate fp;
publicProductApple(FriutPlate fp) {
this.fp= fp;
}
@Override
publicvoidrun() {
try{
synchronized(fp) {
while(true) {
// System.out.println("正在往里放蘋果");
fp.addFriut("蘋果");
}
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
classProductOrange implementsRunnable {
FriutPlate fp;
publicProductOrange(FriutPlate fp) {
this.fp= fp;
}
@Override
publicvoidrun() {
try{
synchronized(fp) {
while(true) {
// System.out.println("正在往里放橘子");
fp.addFriut("橘子");
}
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
classConsumerApple implementsRunnable {
FriutPlate fp;
publicConsumerApple(FriutPlate fp) {
this.fp= fp;
}
@Override
publicvoidrun() {
while(true) {
synchronized(fp) {
String friutName= null;
try{
friutName= fp.getFriut();
} catch(InterruptedException e1) {
// TODOAuto-generated catch block
e1.printStackTrace();
}
if("蘋果".equals(friutName)) {
System.out.println("兒子吃掉蘋果");
fp.setNameNull();
} else{
System.out.println("不是蘋果,兒子不吃");
try{
Thread.sleep(100);
} catch(InterruptedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
classConsumerOrange implementsRunnable {
FriutPlate fp;
publicConsumerOrange(FriutPlate fp) {
this.fp= fp;
}
@Override
publicvoidrun() {
while(true) {
synchronized(fp) {
String friutName= null;
try{
friutName= fp.getFriut();
} catch(InterruptedException e1) {
// TODOAuto-generated catch block
e1.printStackTrace();
}
if("橘子".equals(friutName)) {
System.out.println("女兒吃掉橘子");
fp.setNameNull();
} else{
System.out.println("不是橘子,女兒不吃");
try{
Thread.sleep(100);
} catch(InterruptedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
publicstaticvoidmain(String[] args) {
Demo d= newDemo();
FriutPlate fp= d.newFriutPlate();
ProductApple pa= d.newProductApple(fp);
ProductOrange po= d.newProductOrange(fp);
ConsumerApple ca= d.newConsumerApple(fp);
ConsumerOrange co= d.newConsumerOrange(fp);
Thread t1= newThread(pa);
Thread t2= newThread(po);
Thread t3= newThread(ca);
Thread t4= newThread(co);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
練習:三個線程輪流打印ABC
練習:死鎖演示
1.當線程為2個時,鎖為一個時,很少出現死鎖
2.當線程為3個時,使用notify()喚醒,極大可能出現死鎖
3.當線程A在sync(a)-sync(b)時,線程B在sync(b)-sync(a)時,這種嵌套鎖,一定會出現死鎖
總結
以上是生活随笔為你收集整理的java多线程嵌套_Java多线程进阶的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英国维珍集团采用区块链技术升级理财应用;
- 下一篇: JavaWeb三层架构的理解/三层架构的