I will not talk about it here. For details, refer to what I wrote.ProgramRight! This is through the ArrayLisClass t to add threads in a unified manner, and finally useThe time that the special scheduler thread assigns to the thread based on the priority.
Import java. util. arraylist;
Public class serverdemo {
Public static void main (string [] ARGs ){
/* The priority setting is not reliable, because it mainly depends on the operating system,
* Even if it is the highest priority, it may not be executed first.
* Comment out the priority sentence and you will find the priority of the main thread by default.
* Highest. In my opinion, there are many running results of programs dependent on the underlying implementation.
* Not Sure. This should be taken care of, or why should we introduce synchronization?
* By default, the priority of the created thread is lower than that of the created thread.
* The priority must be to interrupt the thread in the run method so that other
* The thread can also obtain the CPU time! Otherwise, it will wait until the execution ends.
* Gives control of the system.
*/
Thread. currentthread (). setpriority (thread. max_priority );
Scheduler Sch = new scheduler ();
Sch. setdaemon (true );
Sch. Start ();
Testthread T1 = new testthread ("T1 ");
T1.start ();
Sch. addthread (T1); // change the priority of thread T1
Testthread t2 = new testthread ("T2 ");
T2.start ();
Sch. addthread (T2); // change the priority of thread T2
Testthread T3 = new testthread ("T3 ");
T3.start ();
Sch. addthread (T3); // change the priority of thread T3
System. Out. println ("OK ");
}
}
/*
* This is a scheduler thread. It is mainly used to add threads to the array and change their priority in sequence.
* Level to ensure the order of their execution. However, I still find that they are not executed in sequence.
*
*/
Class scheduler extends thread {
Private arraylist <thread> List = new arraylist <thread> ();
Private Static final int default_time_slice = 10;
Private int slice;
Private int activeindex = 0;
Private thread activethread;
Public scheduler (){
This. Slice = default_time_slice;
This. setname ("scheduler ");
}
Public scheduler (INT slice ){
This. Slice = slice;
This. setname ("scheduler ");
}
Public void addthread (thread t ){
If (t = NULL)
System. Out. println ("the thread is null ");
T. setpriority (2 );
List. Add (t );
}
Private void schedulersleep (){
Try {
Thread. Sleep (slice );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
@ Override
Public void run (){
Thread current;
This. setpriority (6); // make sure that the schedulerthread can execute firstly,
// So it can add the thread in list work rightly
While (true ){
Current = findnext ();
Activethread = current; // get the thread from the list
Activeindex = List. indexof (activethread );//???? Activethread or current try to do it
If (current! = NULL) & (current. isalive ())){
Activethread. setpriority (4 );
}
Schedulersleep ();
If (current! = NULL) & (current. isalive ())){
Activethread. setpriority (2 );
}
System. Out. println ("end of" + thread. currentthread (). getname ());
}
}
Thread findnext (){
Thread current;
Int Index = 0;
If (activeindex> = (list. Size ()-1) & list. Size ()! = 0) Index = 0;
Else if (list. Size () = 0) return NULL;
// If the current thread is still in the valid state, continue executing the current thread
Else if (activethread! = NULL & activethread. isalive () Index = activeindex;
Else Index = activeindex + 1;
Current = list. Get (INDEX); // return the specified Thread
Return Current;
}
}
Class testthread extends thread {
Public testthread (string name ){
This. setname (name );
}
Public void run (){
System. Out. println ("current name:" +
This. getname () +
"Priority:" + this. getpriority ());
}
}