java基礎 多執行緒例項

2021-08-02 18:46:48 字數 668 閱讀 7965

乙個簡單的買票多執行緒程式:

public class saleticket2 implements runnable catch (interruptedexception e)

if(ticket>0)}}

} public static void main(string args) }

把同步加到方法上:

public class sellticket implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()

+ "正在**第" + (tickets--) + "張票 ");}}

}else

x++;}}

//如果乙個方法一進去就看到了**被同步了,那麼我就再想能不能把這個同步加在方法上呢?

private static synchronized void sellticket() catch (interruptedexception e)

system.out.println(thread.currentthread().getname()

+ "正在**第" + (tickets--) + "張票 ");}}

}class demo

Java多執行緒例項

thread類的構造方法 public thread public thread runnable target public thread string name public thread runnable target,string name public thread threadgroup...

Java多執行緒wait,notify例項

package com.lj.thread2 public class sample catch interruptedexception e number system.out.println number increased by thread.currentthread getname num...

Java基礎 多執行緒

多執行緒的概念,如何在程式中建立多執行緒 thread runnable 執行緒安全問題,執行緒的同步,執行緒之間的通訊 死鎖。一 建立執行緒和啟動執行緒。為什麼要覆蓋run方法 thread類用於描述執行緒。該類就定義了乙個功能。用於儲存執行緒要執行的 該儲存功能就是run方法。也就是說threa...