answersLogoWhite

0

How to creates threads?

User Avatar

Anonymous

13y ago
Updated: 8/20/2019

solution 01: creating two parallel threads by implementing the Runnable interface, i.e. adding a "run" method.

solution 02: alternatively, we may create two parallel threads by deriving our class from the "Thread" class. in this case, we also have to provide a "run" method, because Threaduses the Runnable interface.

An example for this is...

public class csj01x3 extends Thread { public static void main(String args[]) throws Throwable { new csj01x3().start(); new csj01x3().start(); // main thread is ending here, // Thread-0 and Thread-1 continue to run. } public void run() { try { for (int i=0; i<100; i++) { System.out.println("thread " +Thread.currentThread().getName()+" step "+i); Thread.sleep(500); } } catch (Throwable t) { } } }

this produces an output as solution 01, but it has one tradeoff: as we derive our class from Thread, we can no longer derive it from anything else. therefore solution 01 is more flexible.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is main thread in java program?

the main thread is the first thread that creates when the JVM starts executing your program. All subsequent threads that you may create in your program would be child threads to the main thread. It is important to note that if the main thread exits, all other threads that were spawned by it will also get killed.


What was the sequence of the three colored threads from top to bottom?

The sequence of the three colored threads from top to bottom is blue, red, and green. This arrangement creates a visually appealing contrast and can be used in various design applications. Each color can also symbolize different meanings depending on the context.


Are NPT pipe threads compatible with NPSM threads?

No, NPT threads are not compatible with NPSM threads. A pipe with NPT threads tapers slightly, while an NPSM-threaded pipe does not taper.


Different kind of threads in MFC?

There are two main kinds of threads implementations: User-space threads and Kernel-supported threads. Mikaela


Whats the difference between plumbing pipe threads and conduit pipe threads?

Plumbing pipe threads are squared and conduit pipe threads are tapered.


What are directional threads in fabric?

The long threads are called the warp and the shorter cross threads are called the weft.


What npt pipe threads full name?

National Pipe Threads or the Briggs Standard NFT = National fine threads NCT National corse threads ASAE America society of auto engineer threads etc


What are the differences between Internal and External threads?

Internal threads are threads inside of a hole in something. Like a nut, for instance. External threads are threads on the outside of a cylindrical object. Like a bolt, for instance. hth, Steve


What is a 60's slang word that is equivelent to nice cloths?

Cool threads Boss threads Hip threads


When was Broken Threads created?

Broken Threads was created in 1917.


When did Threads of Fate happen?

Threads of Fate happened in 1999.


When was Campus Threads created?

Campus Threads was created in 2006.