Java Reference
In-Depth Information
Caution Usingthe setPriority() methodcanimpactanapplication'sportab-
ilityacrossplatformsbecausedifferentschedulerscanhandleaprioritychangeindif-
ferentways.Forexample,oneplatform'sschedulermightdelaylowerprioritythreads
fromexecutinguntilhigherprioritythreadsfinish.Thisdelayingcanleadto indefinite
postponement or starvation because lower priority threads “starve” while waiting in-
definitelyfortheirturntoexecute,andthiscanseriouslyhurttheapplication'sperform-
ance.Anotherplatform'sschedulermightnotindefinitelydelaylowerprioritythreads,
improving application performance.
Listing4-18 refactors Listing4-17 ' s main() methodtogiveeachthreadanondefault
name, and to put each thread to sleep after outputting name and count .
Listing 4-18. A pair of counting threads revisited
class CountingThreads
{
public static void main(String[] args)
{
Runnable r = new Runnable()
{
@Override
public void run()
{
String
name
=
Thread.currentThread().getName();
int count = 0;
while (true)
{
System.out.println(name+":
"+count++);
try
{
Thread.sleep(100);
}
catch
(InterruptedException
ie)
{
}
Search WWH ::




Custom Search