python与Java线程实现方式的对比
1. python多線程的實現方式:
- 繼承Thread類
- 實現Runnable接口
Callable接口+FutureTask包裝器來創建Thread線程
線程操作涉及到的方法:
Thread:start啟動 join加入新線程
Lock/Rock:acquire():加鎖 release(): 釋放鎖
Condition:acquire():加鎖 release(): 釋放鎖 wait notify notify_all
Event: set()等同于notify_all clear() 取消notify_all
time: sleep
timer:Timer(interval function args=[] kwargs={})interval: 指定的時間
- function: 要執行的方法
- args/kwargs: 方法的參數
python多線程實現的三種方式詳解
2.Java多線程的實現方式:
- 繼承Thread類實現多線程
- 實現Runnable接口方式實現多線程
- 通過 Callable 和 Future 創建線程。
線程操作涉及到的方法:
start() 、stop() (過時) 、join() 、wait() 、sleep() 、notify() 、notifyAll()
Java多線程方法詳解
JAVA多線程實現的三種方式詳解
經典案例
共有四個和尚
其中一個和尚負責做饅頭
其他三個和尚吃饅頭
要求
當做饅頭的時候,不能吃饅頭
當吃饅頭的時候不能做饅頭
饅頭上線只能是30個
在吃饅頭的時候不能出現一個饅頭被多個和尚吃
不能出現吃的時候和尚吃出異常(如一個和尚永遠也吃不到,或者 一個和尚吃了一個不存在的饅頭)
Java代碼:
package com.dsj101.thread; import java.util.ArrayList; import java.util.List; public class Test { static class ManTou{ private int num; public ManTou(int num) { this.num = num; } @Override public String toString() { return "第"+num+"個"; } } public static List<ManTou> manTous=new ArrayList<>(); public static void main(String[] args) { new HuoFu("大頭和尚").start(); new ChiHuo("白眉和尚").start(); new ChiHuo("牛鼻子和尚").start(); new ChiHuo("花和尚").start(); } //火夫 static class HuoFu extends Thread{ private String name; public HuoFu(String name) { this.name = name; } @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } while (true) { synchronized ("a") { System.out.println(name + "開始蒸饅頭"); for (int i = 0; i < 30; i++) { manTous.add(new ManTou(i + 1)); System.out.println("做完第" + (i + 1) + "個碼頭"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("饅頭做好了,開始吃吧"); "a".notifyAll(); } synchronized ("b") { try { System.out.println("我先去睡一會,沒了叫我"); "b".wait(); System.out.println("我知道了開始做去"); } catch (InterruptedException e) { e.printStackTrace(); } } } } } //吃貨 static class ChiHuo extends Thread{ private String name; public ChiHuo(String name) { this.name = name; } @Override public void run() { synchronized ("a"){ try { "a".wait(); } catch (InterruptedException e) { e.printStackTrace(); } } ManTou m=null; while (true) { synchronized ("a") { System.out.println(name + "開始吃饅頭"); if (manTous.size() != 0) { m = manTous.remove(0); } else { System.out.println(name + "發現沒有饅頭,叫醒火夫做饅頭"); synchronized ("b"){ "b".notify(); } try { System.out.println("我等著呢,完事后叫我吃"); "a".wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if (m != null) { System.out.println(name + "消化饅頭"+m.toString()); int rd = (int) (Math.random() * 2000 + 1000); m = null; try { Thread.sleep(rd); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }python代碼:
import threading import time mantous=[] a=threading.Lock() b=threading.Lock() ca=threading.Condition(a) cb=threading.Condition(b) #火夫的任務函數 def huofu(name): time.sleep(1) while True: ca.acquire() for i in range(1,31): mantous.append("第{0}個饅頭".format(i)) print("做好第{0}個饅頭".format(i)) time.sleep(0.1) print("饅頭做好,叫醒吃貨") ca.notify_all() ca.release() print("{0}去等到".format(name)) cb.acquire() cb.wait() cb.release() #吃貨的任務函數 def chihuo(name): ca.acquire() ca.wait() ca.release() while True: m=None ca.acquire() if len(mantous) != 0 : m=mantous.pop() else: print("沒饅頭了") cb.acquire() cb.notify() cb.release() ca.wait() ca.release() if m is not None: print("{0}吃{1}".format(name,m)) time.sleep(1) class Chuofu(threading.Thread): def __init__(self,name): #threading.Thread.__init__(self) super(Chuofu,self).__init__() self.name=name def run(self): time.sleep(1) while True: ca.acquire() for i in range(1,31): mantous.append("第{0}個饅頭".format(i)) print("做好第{0}個饅頭".format(i)) time.sleep(0.1) print("饅頭做好,叫醒吃貨") ca.notify_all() ca.release() print("{0}去等到".format(self.name)) cb.acquire() cb.wait() cb.release() class Cchihuo(threading.Thread): def __init__(self,name): threading.Thread.__init__(self) self.name=name def run(self): ca.acquire() ca.wait() ca.release() while True: m=None ca.acquire() if len(mantous) != 0 : m=mantous.pop() else: print("沒饅頭了") cb.acquire() cb.notify() cb.release() ca.wait() ca.release() if m is not None: print("{0}吃{1}".format(self.name,m)) time.sleep(1) # threading._start_new_thread(huofu,('大頭和尚',)) # threading._start_new_thread(chihuo,('白眉和尚',)) # threading._start_new_thread(chihuo,('牛鼻子和尚',)) # threading._start_new_thread(chihuo,('花和尚',)) # t1=threading.Thread(target=huofu,args=('大頭和尚',)) # t2=threading.Thread(target=chihuo,args=('白眉和尚',)) # t3=threading.Thread(target=chihuo,args=('牛鼻子和尚',)) # t4=threading.Thread(target=chihuo,args=('花和尚',)) # t1.start() # t2.start() # t3.start() # t4.start() Chuofu("大頭和尚").start() Cchihuo('白眉和尚').start() Cchihuo('牛鼻子和尚').start() Cchihuo('花和尚').start() input()轉載于:https://www.cnblogs.com/cn-7876/p/7757776.html
總結
以上是生活随笔為你收集整理的python与Java线程实现方式的对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python类库的查找
- 下一篇: python3安装scrapy问题解决