[size=medium]程式功能:4個執行緒,2個執行緒對某個數值進行加1;2個執行緒進行減1,要求該數在0和1之間切換。程式如下,大家看看問題出在**:[/size]
decthread類,對sample的i進行減1的執行緒:
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run() }}
public class decthread extends thread
@override
public void run()
}}
incthread 類,對sample的i進行加1的執行緒:
public class incthread extends thread
@override
public void run()
}}
sample類:
public class sample
i++;
system.out.println(thread.currentthread().getname() + "-" + i);
notify();
} catch (interruptedexception e)
} public synchronized void dec()
i--;
system.out.println(thread.currentthread().getname() + "-" + i);
notify();
} catch (interruptedexception e)
}}
測試類testincdec :
public class testincdec
}
Java中建立多執行緒
建立乙個runnable物件runable threadjob new myrunnable runnable相當於執行緒中所要執行的工作。我們需要新建乙個類繼承runnable介面,並重寫它唯一的方法run run 方法被放在新執行緒的棧底,是被第乙個執行的方法。2.建立乙個thread類的物件,...
java中的多執行緒
package testthread 店員從生產者取貨,消費者從店員取貨,店員最多只能存放20個產品,當產品不夠20個需通知生產者生產,超過20個時停止消費 author passenger 店員類 class clerk catch interruptedexception e else 消費產品...
java中的多執行緒
建立執行緒的第一種方法 繼承thread類。步驟 1,定義乙個類繼承thread 2,複寫thread類中的run方法 目的 將自定義 儲存在run方法中,讓執行緒執行 3,呼叫執行緒的start方法,該方法用兩個作用 啟動執行緒,呼叫run方法 多執行緒的特性 隨機性 示例 class demo ...