Creating a’Thread Java Help

Creating a’Thread
In the most general sense, you’ create a instantiating an object of type Thread. Java defines two ways in which this can be’ accomplished:
• You can implement the Runnable interface.
• You can extend the Thread class, itself.
The following two sections look at each method’, in turn.

Implementing Runnable
The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable, To implement Runnable, a class need only’ implement a single method called runt ), which is declared like this: public void run( ) Inside run(), you will define the code that constitutes the new thread. It is important to understand that runt ) can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that runt ) establishes the entry point for another, concurrent thread of execution within your program. This thread will end when runt ) returns. After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors, The one that we will use is shown here: Thread(Runnable threndOb, String threadName) In this constructor; threndOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. The name of the new thread is specified by thread blame. After the new thread is created, it will not start running until you call its start( ). method, which is declared within Thread. In essence, start( ) executes a call to run( ). The start( ) method is shown here:

void start( )

Here is an example that creates a new thread and starts it running:

Inside New Thread’s constructor, a new Thread object is created by the following statement:

t = new Thread(this,     “Demo Thread”

Passing this as the first argument indicates that you want the new thread to the runt ) method on this object. Next, start() is called, which starts the thread of execution beginning at the run() method. This causes the child thread’s for loop to begin. After caLling start(), New Thread’s constructor returns to main(). When the main thread resumes, it enters its for loop. Both threads continue running, sharing The CPU,’until. their loops finish, The output produced by this program is as follows

Child thread: Thread[Demo Thread.5.main]
Main ‘thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread’: 3
Child Thread: 1

Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

As mentioned earner, in a multithreaded program, the main thread must be the last thread to finish running: ff the main thread finishes before a child thread has completed, then the Java run-time system may “hang,” The preceding program ensures that the main thread finishes last, because the main thread sleeps for 1,000 milliseconds between it tions, but the child thread sleeps for only 500 milliseconds, This causes the child thread to terminate earlier than the main thread. Shortly, you will see a better wny to ensure that t~ main thread finishes last.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in The Complete Reference

Share the Story

About the Author

Back to Top