Java Thread队列一直是我们需要掌握的代码下面我们就基本思想建立了一个队列为每一个Java Thread队列保存了一个对象锁保证按顺序执行线程启动的时候使随机的但是执行代码是按顺序的
import javautilLinkedList;
import javautilQueue;
public class ThreadTest {
private static Queue qThread=new LinkedList();//线程同步对象队列
public static synchronized void putObject(Object t){
qThreadoffer(t);
}
public static synchronized Object getObject(){
return qThreadpoll();
}
public static void waitThread(Object t) throws InterruptedException{
synchronized(t){
twait();
}
}
public static void notifyThread(){
Object obj=ThreadTestgetObject();
synchronized(obj){
objnotify();
}
}
public static void main(String[] args) throws InterruptedException {
int i = ;
boolean isFirst=true;
while (i < ) {
Object obj=new Object();
if(i>){
isFirst=false;
ThreadTestputObject(obj);
}
Thread t = new Thread(isFirstobj);
Object obj=new Object();
ThreadTestputObject(obj);
Thread t = new Thread(obj);
tstart();
tstart();
i++;
}
}
}
/**
* 线程
*
* @author HarryWANG
*
*/
class Thread extends Thread {
private boolean isFirst=false;
private Object obj;
public Thread(boolean fObject obj){
thisisFirst=f;
thisobj=obj;
}
@Override
public void run() {
if(!thisisFirst){
Systemoutprintln(thisgetName()+等待);
try{
ThreadTestwaitThread(obj);
}catch(InterruptedException e){
eprintStackTrace();
}
}
Systemoutprintln(启动+thisgetName()+);
try {
sleep();//等待秒为了测试
} catch (InterruptedException e) {
eprintStackTrace();
}
Systemoutprintln(停止+thisgetName()+);
ThreadTestnotifyThread();
}
}
class Thread extends Thread {
private Object obj;
public Thread(Object obj){
thisobj=obj;
}
@Override
public void run() {
Systemoutprintln(thisgetName()+等待);
try{
ThreadTestwaitThread(obj);
}catch(InterruptedException e){
eprintStackTrace();
}
Systemoutprintln(启动+thisgetName()+);
try {
sleep();//等待秒为了测试
} catch (InterruptedException e) {
eprintStackTrace();
}
Systemoutprintln(停止+thisgetName()+);
ThreadTestnotifyThread();
}
}
以上就是对Java Thread队列的详细介绍希望大家有所帮助