Threading Java

22
Introduction to Threads Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously. Multithreading has several advantages over Multiprocessing such as; Threads are lightweight compared to processes Threads share the same address space and therefore can share both data and code Context switching between threads is usually less expensive than between processes Cost of thread intercommunication is relatively low that that of process intercommunication Threads allow different tasks to be performed concurrently. The following figure shows the methods that are members of the Object and Thread Class. Thread Creation There are two ways to create thread in java; Implement the Runnable interface (java.lang.Runnable) By Extending the Thread class (java.lang.Thread)

description

Thread in java

Transcript of Threading Java

Page 1: Threading Java

Introduction to ThreadsMultithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.

Multithreading has several advantages over Multiprocessing such as;

Threads are lightweight compared to processes Threads share the same address space and therefore can share both data and code Context switching between threads is usually less expensive than between processes Cost of thread intercommunication is relatively low that that of process

intercommunication Threads allow different tasks to be performed concurrently.

The following figure shows the methods that are members of the Object and Thread Class.

Thread Creation

There are two ways to create thread in java;

Implement the Runnable interface (java.lang.Runnable) By Extending the Thread class (java.lang.Thread)

Implementing the Runnable Interface

The Runnable Interface Signature

public interface Runnable {

void run();

}

Page 2: Threading Java

One way to create a thread in java is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.

The procedure for creating threads based on the Runnable interface is as follows:

1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object.

2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.

3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.

4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.

Below is a program that illustrates instantiation and running of threads using the runnable interface instead of extending the Thread class. To start the thread you need to invoke the start() method on your object.

class RunnableThread implements Runnable {

Thread runner;public RunnableThread() {}public RunnableThread(String threadName) {

runner = new Thread(this, threadName); // (1) Create a new thread.System.out.println(runner.getName());runner.start(); // (2) Start the thread.

}public void run() {

//Display info about this particular threadSystem.out.println(Thread.currentThread());

}}

public class RunnableExample {

public static void main(String[] args) {Thread thread1 = new Thread(new RunnableThread(), "thread1");Thread thread2 = new Thread(new RunnableThread(), "thread2");RunnableThread thread3 = new RunnableThread("thread3");//Start the threadsthread1.start();thread2.start();try {

//delay for one secondThread.currentThread().sleep(1000);

} catch (InterruptedException e) {

Page 3: Threading Java

}//Display info about the main threadSystem.out.println(Thread.currentThread());

}}

Output

thread3Thread[thread1,5,main]Thread[thread2,5,main]Thread[thread3,5,main]Thread[main,5,main]private

Download Runnable Thread Program Example

This approach of creating a thread by implementing the Runnable Interface must be used whenever the class being used to instantiate the thread object is required to extend some other class.

Extending Thread Class

The procedure for creating threads based on extending the Thread is as follows:

1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.

2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.

3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.

Below is a program that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the start() method on your object.

class XThread extends Thread {

XThread() {

Page 4: Threading Java

}XThread(String threadName) {

super(threadName); // Initialize thread.System.out.println(this);start();

}public void run() {

//Display info about this particular threadSystem.out.println(Thread.currentThread().getName());

}}

public class ThreadExample {

public static void main(String[] args) {Thread thread1 = new Thread(new XThread(), "thread1");Thread thread2 = new Thread(new XThread(), "thread2");// The below 2 threads are assigned default namesThread thread3 = new XThread();Thread thread4 = new XThread();Thread thread5 = new XThread("thread5");//Start the threadsthread1.start();thread2.start();thread3.start();thread4.start();try {

//The sleep() method is invoked on the main thread to cause a one second delay.Thread.currentThread().sleep(1000);

} catch (InterruptedException e) {}//Display info about the main threadSystem.out.println(Thread.currentThread());

}}

Output

Thread[thread5,5,main]thread1thread5thread2Thread-3Thread-2Thread[main,5,main]

Download Java Thread Program Example

Page 5: Threading Java

When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:

Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interfacehas this option.

A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive.

An example of an anonymous class below shows how to create a thread and start it:

( new Thread() {

public void run() {

for(;;) System.out.println(“Stop the world!”);

}

}

).start();

IntroductionMultithreading is the ability to do multiple things at once with in the same application. It provides finergranularity of concurrency. A thread -- sometimes called an execution context or a lightweight process --is a single sequential flow of control within a program.Threads are light weight as compared to processes because they take fewer resources then a process. Athread is easy to create and destroy. Threads share the same address spacei.e. multiple threads can share the memory variables directly, and therefore may require more complexsynchronization logic to avoid deadlocks and starvation.Sequential Execution vs MultithreadingEvery program has atleast one thread. Programs without multithreading executes sequentially. That is, afterexecuting one instruction the next instruction in sequence is executed. If a function is called then until thecompletion of the function the next instruction is not executed. Similarly if there is a loop then instructionsafter loop only gets executed when the loop gets completed. Consider the following java program havingthree loops in it.// File ThreeLoopTest.java

Page 6: Threading Java

public class ThreeLoopTest {public static void main (String args[ ]) {//first loopfor (int i=1; i<= 5; i++)System.out.println("first " +i);// second loopfor (int j=1; j<= 5; j++)System.out.println("second " + j);// third loopfor (int k=1; k<= 5; k++)System.out.println("third " + k);} // end main} // end classNote: Each loop has 5 iterations in the ThreeLoopTest program.168

Page 7: Threading Java
Page 8: Threading Java

Web Design & Development CS506VUNote: Each loop has 10 iterations in the ThreadTest program. Your output can be different fromthe one given above.Notice the difference between the outputs of the two programs. In ThreeLoopTest each loop generated asequential output while in ThreadTest the output of the loops got intermingled i.e. concurrency took placeand loops executed simultaneouslyLet us code our first multithreaded program and try to learn how Java supports multithreading.Java includes built-in support for threading. While other languages have threads bolted-on to an existingstructure. i.e. threads were not the part of the original language but latter came into existence as the needarose.All well known operating systems these days support multithreading. JVM transparently maps JavaThreads to their counter-parts in the operating system i.e. OS Threads. JVM allows threads in Java to takeadvantage of hardware and operating system level advancements. It keeps track of threads and schedulesthem to get CPU time. Scheduling may be pre-emptive or cooperative. So it is the job of JVM to managedifferent tasks of thread. Let's see how we can create threads?Creating Threads in JavaThere are two approaches to create threads in Java.Using InterfaceUsing InheritanceFollowing are the steps to create threads by using Interface:1Create a class where you want to put some code that can run in parallel with some other code andlet that class implement the Runnable interface2Runnable interface has the run() method therefore provide the implementation for the run() methodand put your code that you want to run in parallel here169

Page 9: Threading Java
Page 10: Threading Java

Web Design & Development CS506VU3Instantiate Thread class object by passing Runnable object in constructor4Start thread by calling start() methodFollowing are the steps to create threads by using Intheritance:1Inherit a class from java.lang.Thread class2Override the run() method in the subclass3Instantiate the object of the subclass4Start thread by calling start() methodTo write a multithreaded program using Runnable interface, follow these steps:·  Step 1 - Implement the Runnable Interfaceclass Worker implements Runnable·  Step 2 - Provide an Implementation of run() methodpublic void run( ){// write thread behavior// code that will be executed by the thread·  Step 3 - Instantiate Thread class object by passing Runnable object in the constructorWorker w = new Worker("first");Thread t = new Thread (w);·Step 4 Start thread by calling start() methodt.start();Threads Creation Steps Using InheritanceTo write a multithreaded program using inheritance from Thread class, follow these steps:Step 1 Inherit from Thread Classclass Worker extends ThreadStep 2 - Override run() methodpublic void run( ){// write thread behavior// code that will execute by threadStep 3 - Instantiate subclass objectStep 4 Start thread by calling start() methodWorker w = new Worker("first");t.start();So far we have explored:What is multithreading?What are Java Threads?Two ways to write multithreaded Java programsNow we will re-write the ThreeLoopTest program by using Java Threads. At first we will use the Interfaceapproach and then we will use Inheritance.Code Example using Interface// File Worker.javapublic class Worker implements Runnable {private String job ;//Constructor of Worker classpublic Worker (String j ){job = j;}//Implement run() method of Runnable interfacepublic void run ( ) {for(int i=1; i<= 10; i++)System.out.println(job + " = " + i);170

Page 11: Threading Java
Page 12: Threading Java

Web Design & Development CS506VU}} // end class// File ThreadTest.javapublic class ThreadTest{public static void main (String args[ ]){//instantiate three objectsWorker first = new Worker ("first job");Worker second = new Worker ("second job");Worker third = new Worker ("third job");//create three objects of Thread class & passing worker//(runnable) to themThread t1 = new Thread (first );Thread t2 = new Thread (second);Thread t3 = new Thread (third);//start threads to executet1.start();t2.start();t3.start();}//end main} // end classFollowing code is similar to the code given above, but uses Inheritance instead of interface// File Worker.javapublic class Worker extends Thread{private String job ;//Constructor of Worker classpublic Worker (String j ){job = j;}//Override run() method of Thread classpublic void run ( ) {for(int i=1; i<= 10; i++)System.out.println(job + " = " + i);}} // end class// File ThreadTest.javapublic class ThreadTest{public static void main (String args[ ]) {//instantiate three objects of Worker (Worker class is now//becomes a Thread because it is inheriting from it)classWorker first = new Worker ("first job");Worker second = new Worker ("second job");Worker third = new Worker ("third job");//start threads to executet1.start();t2.start();t3.start();}//end main} // end classThreads provide a way to write concurrent programs. But on a single CPU, all the threads do not runsimultaneously. JVM assigns threads to the CPU based on thread priorities. Threads with higher priority areexecuted in preference to threads with lower priority. A thread's default priority is same as that of thecreating thread i.e. parent thread.A Thread's priority can be any integer between 1 and 10. We can also use the following predefinedconstants to assign priorities.171

Page 13: Threading Java
Page 14: Threading Java

Web Design & Development CS506VUThread.MAX_PRIORITY (typically 10)Thread.NORM_PRIORITY (typically 5)Thread.MIN_PRIORITY (typically 1)To change the priority of a thread, we can use the following methodsetPriority(int priority)It changes the priority of this thread to integer value that is passed. It throws an IllegalArgumentExceptionif the priority is not in the range MIN_PRIORITY to MAX_PRIORITY i.e. (110).For example, we can write the following code to change a thread's priority.Thread t = new Thread (RunnableObject);// by using predefined constantt.setPriority(Thread.MAX_PRIORITY);// by using integer constantt.setPriority (7);Thread Priority SchedulingThe Java runtime environment supports a very simple, deterministic scheduling algorithm called fixed-priority scheduling. This algorithm schedules threads on the basis of their priority relative to otherRunnable threads.At any given time, when multiple threads are ready to be executed, the runtime system chooses forexecution the Runnable thread that has the highest priority. Only when that thread stops, yields (will beexplained later), or becomes Not Runnable will a lower-priority thread start executing. If two threads of thesame priority are waiting for the CPU, the scheduler arbitrarily chooses one of them to run. The chosenthread runs until one of the following conditions becomes true:A higher priority thread becomes Runnable.It yields, or its run() method exits.On systems that support time-slicing, its time allotment has expired.Then the second thread is given a chance to run, and so on, until the interpreter exits. Consider thefollowing figure in which threads of various priorities are represented by capital alphabets A, B, ..., K. Aand B have same priority (highest in this case). J and K have same priority (lowest in this case). JVM startexecuting with A and B, and divides CPU time between these two threads arbitrarily. When both A and Bcomes to an end, it chooses the next thread C to execute.Code Example: Thread PropertiesTry following example to understandhow JVM executes threads based ontheir priorities.// File PriorityEx.javapublic class PriorityEx{ public staticvoid main (String args[ ]){//instantiate two objectsWorker first = new Worker("first job");Worker second = new Worker("second job");//create two objectsThread t1 = new Thread (first );Thread t2 = new Thread(second);//set thread prioritiest1.setPriority172

Page 15: Threading Java
Page 16: Threading Java

Web Design & Development CS506VU(Thread.MIN_PRIORITY);t2.setPriority (Thread.MAX_PRIORITY);//start threads to executet1.start();t2.start();}//end main} // end classOutputProblems with Thread PrioritiesHowever, when using priorities withJava Threads, remember the followingtwo issues:First a Java thread priority may mapdifferently to the thread priorities ofthe underlying OS. It is because ofdifference in priority levels of JVMand underlying OS. For example32

Solaris has 2 1 prioritylevelsWindows NT has only 7 userpriority levelsSecond, starvation can occur forlower-priority threads if the higher-priority threads never terminate, sleep, or wait for I/O indefinitely.173

Page 17: Threading Java
Page 18: Threading Java