devxlogo

Why Does Spawned Thread Work on PC and not Sun?

Why Does Spawned Thread Work on PC and not Sun?

Question:
This may be too vague, but:

I spawn a thread (after a button click) which bringsup a new window. This works great on my PC runningNetscape 3.0 and Internet Explorer, but it doesn’twork on a Sun (Netscape 3.0). I’ve also tried iton another PC running Internet Explorer and it didn’t workthere, either.

Any clues?

Answer:
I’m guessing the parent thread goes on todo other work, while the window created by thechild thread never appears. If that’s true, theproblem is probably a selfish parent.

There are two styles of multithreading: cooperativeand preemptive. In cooperative multithreading, athread controls the CPU until it voluntarilygives it up, usually by performing I/O orby explicitly giving it up with a call to sleep()or wait(). A thread that never relinquishes controlof the CPU is called a selfish thread.

In preemptive multithreading, the OSperiodically interrupts (preempts) the threadcontrolling the CPU, places it at the end of thequeue of runnable threads, and gives the nextrunnable thread control of the CPU. Thistechnique is also called time slicing. The multi-threading facilities on Solaris and Windows 3.1 arecooperative, while the facilities on Windows NT andWindows 95 are preemptive.

Since Java accomplishes multithreading by using themultithreading facilities of the host OS, one canobserve system dependent behavior in a multithreadedapplication.

Here’s a selfish parent thread:

class Parent extends Thread {      public void run() {         Child c = new Child();         c.start();         for(;;);      }   } 
Immediately after spawning a child, c, the parent enters atight infinite loop, never voluntarilyreleasing the CPU.

Assume the child merely displays a string:

   class Childextends Thread {      public void run() {         System.out.println(“Hello World”);      }   } 
On Solaris or Windows 3.1, we’ll never see the child’spitiful output. On Windows NT and Windows 95, we do seethe child’s message because the OS will periodicallypreempt the selfish parent to give the child a chanceat the CPU.
See also  Why ChatGPT Is So Important Today

We can turn the selfish parent into a cooperative parentby having it periodically release the CPU for a fewmilliseconds using the sleep() or wait()methods:

   class Parent extends Thread {      public void run() {         Child c = new Child();         c.start();         for(;;) {           try {sleep(5);}  // sleep for 5 milliseconds           catch(InterruptedException e) {System.exit(1);}         }      }   } 

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist