Java Thread隊(duì)列一直是我們需要掌握的代碼 下面我們就基本思想 建立了一個(gè)隊(duì)列 為每一個(gè)Java Thread隊(duì)列保存了一個(gè)對(duì)象鎖 保證按順序執(zhí)行 線程啟動(dòng)的時(shí)候 使隨機(jī)的 但梁桐運(yùn)是執(zhí)行代碼是按順序的

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的松江網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
import java util LinkedList;
import java util Queue;
public class ThreadTest {
private static Queue qThread=new LinkedList();//線程同步對(duì)象隊(duì)列
public static synchronized void putObject(Object t){
qThread offer(t);
}
public static synchronized Object getObject(){
return qThread poll();
}
public static void waitThread(Object t) throws InterruptedException{
synchronized(t){
t wait();
}
}
public static void notifyThread(){
Object obj=ThreadTest getObject();
synchronized(obj){
obj notify();
}
}
public static void main(String[] args) throws InterruptedException {
int i = ;
boolean isFirst=true;
while (i ) {
Object obj=new Object();
if(i ){
isFirst=false;
ThreadTest putObject(obj);
}
Thread t = new Thread (isFirst obj);
Object obj =new Object();
ThreadTest putObject(obj );
Thread t = new Thread (obj );
t start();
t start();
i++;
}
}
}
/**
* 線程
橡梁 *
* @author Harry WANG
*
*/
class Thread extends Thread {
private boolean isFirst=false;
private Object obj;
public Thread (boolean f Object obj){
this isFirst=f;
this obj=obj;
}
@Override
public void run() {
if(!this isFirst){
System out println(this getName()+ 等待 );
try{
ThreadTest waitThread(obj);
}catch(InterruptedException e){
e printStackTrace();
}
}
System out println( 啟動(dòng) +this getName()+ );
try {
sleep( );//等待 秒 為了測(cè)試
} catch (InterruptedException e) {
輪租 e printStackTrace();
}
System out println( 停止 +this getName()+ );
ThreadTest notifyThread();
}
}
class Thread extends Thread {
private Object obj;
public Thread (Object obj){
this obj=obj;
}
@Override
public void run() {
System out println(this getName()+ 等待 );
try{
ThreadTest waitThread(obj);
}catch(InterruptedException e){
e printStackTrace();
}
System out println( 啟動(dòng) +this getName()+ );
try {
sleep( );//等待 秒 為了測(cè)試
} catch (InterruptedException e) {
e printStackTrace();
}
System out println( 停止 +this getName()+ );
ThreadTest notifyThread();
}
}
lishixinzhi/Article/program/Java/hx/201311/27018
新聞名稱:java代碼關(guān)閉推送隊(duì)列 java關(guān)閉彈窗
標(biāo)題路徑:http://chinadenli.net/article8/dspgjop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、App設(shè)計(jì)、App開發(fā)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)