<title>Comity of Threads</title>
thread comity means that the currently running thread exits the running state and temporarily gives the right to the same or higher priority thread.
call the yield () method to implement Comity, and he will move the current program to the ready state.
The yield () method does not throw any exceptions.
class Test1
Implements Runnable
{ Public void Run
(){
for
(int I
= 1
;I
<= 5
;I
++) {System
. out
.println
("Running thread:"
+ Thread
.CurrentThread
().GetName
()
+ ", i ="
+ I
);Thread
.CurrentThread
().yield
();
}
}
}class Test2
Implements Runnable
{ Public void Run
(){
for
(int I
= 1
;I
<= 5
;I
++) {System
. out
.println
("Running thread:"
+ Thread
.CurrentThread
().GetName
()
+ ", i ="
+ I
);
}
}
} Public class ThreadDemo11
{ Public Static void Main
(String
[] args
){Test1 T1
=
New Test1
();Thread Th1
=
New Thread
(T1
,"Thread-1"
);Test2 T2
=
New Test2
();Thread Th2
=
New Thread
(T2
,"Thread-2"
);Th1
.Start
();Th2
.Start
();
}
}
Theoretically, T1 threads are less likely to run than T2 threads, because T1 always lets other threads execute first
From for notes (Wiz)
Comity of Threads