Software Development Exam  >  Software Development Notes  >  Java Programming Basics  >  Multi Threading , Java

Multi Threading , Java | Java Programming Basics - Software Development PDF Download

Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

A multithreading is a specialized form of multitasking. Multitasking threads require less overhead than multitasking processes.

I need to define another term related to threads: process: A process consists of the memory space allocated by the operating system that can contain one or more threads. A thread cannot exist on its own; it must be a part of a process. A process remains running until all of the non-daemon threads are done executing.

Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.

Life Cycle of a Thread:

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread.

Java Thread

Above mentioned stages are explained here:

  • New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

  • Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

  • Waiting: Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

  • Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

  • Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Thread Priorities:

Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.

Java priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).

Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependentant.

Creating a Thread:

Java defines two ways in which this can be accomplished:

  • You can implement the Runnable interface.

  • You can extend the Thread class, itself.

Create Thread by Implementing Runnable:

The easiest way to create a thread is to create a class that implements the Runnable interface.

To implement Runnable, a class need only implement a single method called run( ), which is declared like this:

public void run( )

You will define the code that constitutes the new thread inside run() method. It is important to understand that run() can call other methods, use other classes, and declare variables, just like the main thread can.

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 threadOb, String threadName);

Here threadOb is an instance of a class that implements the Runnable interface and the name of the new thread is specified by threadName.

After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread. The start( ) method is shown here:

void start( );

Example:

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

// Create a new thread.

class NewThread implements Runnable {

  Thread t;

  NewThread() {

     // Create a new, second thread

     t = new Thread(this, "Demo Thread");

     System.out.println("Child thread: " + t);

     t.start(); // Start the thread

  }

  

  // This is the entry point for the second thread.

  public void run() {

     try {

        for(int i = 5; i > 0; i--) {

           System.out.println("Child Thread: " + i);

           // Let the thread sleep for a while.

           Thread.sleep(500);

        }

    } catch (InterruptedException e) {

        System.out.println("Child interrupted.");

    }

    System.out.println("Exiting child thread.");

  }

}

 

class ThreadDemo {

  public static void main(String args[]) {

     new NewThread(); // create a new thread

     try {

        for(int i = 5; i > 0; i--) {

          System.out.println("Main Thread: " + i);

          Thread.sleep(1000);

        }

     } catch (InterruptedException e) {

        System.out.println("Main thread interrupted.");

     }

     System.out.println("Main thread exiting.");

  }

}

This would produce following result:

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.

Create Thread by Extending Thread:

The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class.

The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.

Example:

Here is the preceding program rewritten to extend Thread:

// Create a second thread by extending Thread

class NewThread extends Thread {

  NewThread() {

     // Create a new, second thread

     super("Demo Thread");

     System.out.println("Child thread: " + this);

     start(); // Start the thread

  }

 

  // This is the entry point for the second thread.

  public void run() {

     try {

        for(int i = 5; i > 0; i--) {

           System.out.println("Child Thread: " + i);

// Let the thread sleep for a while.

           Thread.sleep(500);

        }

     } catch (InterruptedException e) {

        System.out.println("Child interrupted.");

     }

     System.out.println("Exiting child thread.");

  }

}

 

class ExtendThread {

  public static void main(String args[]) {

     new NewThread(); // create a new thread

     try {

        for(int i = 5; i > 0; i--) {

           System.out.println("Main Thread: " + i);

           Thread.sleep(1000);

        }

     } catch (InterruptedException e) {

        System.out.println("Main thread interrupted.");

     }

     System.out.println("Main thread exiting.");

  }

}

This would produce following result:

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.

Thread Methods:

Following is the list of important medthods available in the Thread class.

Methods with Description

1. public void start()
Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.

2. public void run()
If this Thread object was instantiated using a separate Runnable target, the run() method is invoked on that Runnable object.

3. public final void setName(String name)
Changes the name of the Thread object. There is also a getName() method for retrieving the name.

4. public final void setPriority(int priority)
Sets the priority of this Thread object. The possible values are between 1 and 10.

5. public final void setDaemon(boolean on)
A parameter of true denotes this Thread as a daemon thread.

6. public final void join(long millisec)
The current thread invokes this method on a second thread, causing the current thread to block until the second thread terminates or the specified number of milliseconds passes.

7. public void interrupt()
Interrupts this thread, causing it to continue execution if it was blocked for any reason.

8. public final boolean isAlive()
Returns true if the thread is alive, which is any time after the thread has been started but before it runs to completion.

The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread

Methods with Description

1. public static void yield()
Causes the currently running thread to yield to any other threads of the same priority that are waiting to be scheduled

2. public static void sleep(long millisec)
Causes the currently running thread to block for at least the specified number of milliseconds

3. public static boolean holdsLock(Object x)
Returns true if the current thread holds the lock on the given Object.

4. public static Thread currentThread()
Returns a reference to the currently running thread, which is the thread that invokes this method.

5. public static void dumpStack()
Prints the stack trace for the currently running thread, which is useful when debugging a multithreaded application.

Example:

The following ThreadClassDemo program demonstrates some of these methods of the Thread class:

// File Name : DisplayMessage.java

// Create a thread to implement Runnable

public class DisplayMessage implements Runnable

{

  private String message;

  public DisplayMessage(String message)

  {

     this.message = message;

  }

  public void run()

  {

     while(true)

     {

        System.out.println(message);

     }

  }

}

 

// File Name : GuessANumber.java

// Create a thread to extentd Thread

public class GuessANumber extends Thread

{

  private int number;

  public GuessANumber(int number)

  {

     this.number = number;

  }

  public void run()

  {

     int counter = 0;

     int guess = 0;

     do

     {

         guess = (int) (Math.random() * 100 + 1);

         System.out.println(this.getName()

                      + " guesses " + guess);

         counter++;

     }while(guess != number);

     System.out.println("** Correct! " + this.getName()

                      + " in " + counter + " guesses.**");

  }

}

 

// File Name : ThreadClassDemo.java

public class ThreadClassDemo

{

  public static void main(String [] args)

  {

     Runnable hello = new DisplayMessage("Hello");

     Thread thread1 = new Thread(hello);

     thread1.setDaemon(true);

     thread1.setName("hello");

     System.out.println("Starting hello thread...");

     thread1.start();

     

     Runnable bye = new DisplayMessage("Goodbye");

     Thread thread2 = new Thread(hello);

     thread2.setPriority(Thread.MIN_PRIORITY);

     thread2.setDaemon(true);

     System.out.println("Starting goodbye thread...");

     thread2.start();

 

     System.out.println("Starting thread3...");

     Thread thread3 = new GuessANumber(27);

     thread3.start();

     try

     {

        thread3.join();

     }catch(InterruptedException e)

     {

        System.out.println("Thread interrupted.");

     }

     System.out.println("Starting thread4...");

     Thread thread4 = new GuessANumber(75);

     

 thread4.start();

     System.out.println("main() is ending...");

  }

}

This would produce following result. You can try this example again and again and you would get different result every time.

Starting hello thread...

Starting goodbye thread...

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Thread-2 guesses 27

Hello

** Correct! Thread-2 in 102 guesses.**

Hello

Starting thread4...

Hello

Hello

..........remaining result produced.

 

Threads Synchronization

When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time.

The process by which this synchronization is achieved is called thread synchronization.

The synchronized keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.

This is the general form of the synchronized statement:

synchronized(object) {

  // statements to be synchronized

}

Here, object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor.

Here is an example, using a synchronized block within the run( ) method:

// File Name : Callme.java

// This program uses a synchronized block.

class Callme {

  void call(String msg) {

     System.out.print("[" + msg);

     try {

        Thread.sleep(1000);

     } catch (InterruptedException e) {

        System.out.println("Interrupted");

     }

     System.out.println("]");

  }

}

 

// File Name : Caller.java

class Caller implements Runnable {

  String msg;

  Callme target;

  Thread t;

  public Caller(Callme targ, String s) {

     target = targ;

     msg = s;

     t = new Thread(this);

     t.start();

  }

  

  // synchronize calls to call()

  public void run() {

     synchronized(target) { // synchronized block

        target.call(msg);

     }

  }

}

// File Name : Synch.java

class Synch {

  public static void main(String args[]) {

     Callme target = new Callme();

     Caller ob1 = new Caller(target, "Hello");

     Caller ob2 = new Caller(target, "Synchronized");

     Caller ob3 = new Caller(target, "World");

  

     // wait for threads to end

     try {

        ob1.t.join();

        ob2.t.join();

        ob3.t.join();

     } catch(InterruptedException e) {

        System.out.println("Interrupted");

     }

  }

}

This would produce following result:

[Hello]

[World]

[Synchronized]


 

InterThread Communication

Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.

In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.

To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods:

  • wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).

  • notify( ): This method wakes up the first thread that called wait( ) on the same object.

  • notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.

These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized context.

These methods are declared within Object. Various forms of wait( ) exist that allow you to specify a period of time to wait.

Example:

The following sample program consists of four classes: Q, the queue that you're trying to synchronize; Producer, the threaded object that is producing queue entries; Consumer, the threaded object that is consuming queue entries; and PC, the tiny class that creates the single Q, Producer, and Consumer.

The proper way to write this program in Java is to use wait( ) and notify( ) to signal in both directions, as shown here:

class Q {

  int n;

  boolean valueSet = false;

  synchronized int get() {

     if(!valueSet)

     try {

        wait();

     } catch(InterruptedException e) {

        System.out.println("InterruptedException caught");

     }

     System.out.println("Got: " + n);

     valueSet = false;

     notify();

     return n;

  }

 

  synchronized void put(int n) {

     if(valueSet)

     try {

        wait();

     } catch(InterruptedException e) {

        System.out.println("InterruptedException caught");

     }

     this.n = n;

     valueSet = true;

     System.out.println("Put: " + n);

     notify();

  }

}

 

class Producer implements Runnable {

  Q q;

  Producer(Q q) {

     this.q = q;

     new Thread(this, "Producer").start();

  }

 

  public void run() {

     int i = 0;

     while(true) {

        q.put(i++);

     }

  }

}

 

class Consumer implements Runnable {

   Q q;

   Consumer(Q q) {

      this.q = q;

      new Thread(this, "Consumer").start();

   }

   public void run() {

      while(true) {

      q.get();

   }

 }

}

class PCFixed {

  public static void main(String args[]) {

     Q q = new Q();

     new Producer(q);

     new Consumer(q);

     System.out.println("Press Control-C to stop.");

  }

}

Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready.

When this happens, execution inside get( ) resumes. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue.

Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it.

Here is some output from this program, which shows the clean synchronous behavior:

Put: 1

Got: 1

Put: 2

Got: 2

Put: 3

Got: 3

Put: 4

Got: 4

Put: 5

Got: 5

 

Suspend(),Resume() and Stop() – Threads Control

 

While the suspend( ), resume( ), and stop( ) methods defined by Thread class seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs and obsolete in newer versions of Java.

The following example illustrates how the wait( ) and notify( ) methods that are inherited from Object can be used to control the execution of a thread.

This example is similar to the program in the previous section. However, the deprecated method calls have been removed. Let us consider the operation of this program.

The NewThread class contains a boolean instance variable named suspendFlag, which is used to control the execution of the thread. It is initialized to false by the constructor.

The run( ) method contains a synchronized statement block that checks suspendFlag. If that variable is true, the wait( ) method is invoked to suspend the execution of the thread. The mysuspend( ) method sets suspendFlag to true. The myresume( ) method sets suspendFlag to false and invokes notify( ) to wake up the thread. Finally, the main( ) method has been modified to invoke the mysuspend( ) and myresume( ) methods.

Example:

// Suspending and resuming a thread for Java 2

class NewThread implements Runnable {

  String name; // name of thread

  Thread t;

  boolean suspendFlag;

  NewThread(String threadname) {

     name = threadname;

     t = new Thread(this, name);

     System.out.println("New thread: " + t);

     suspendFlag = false;

     t.start(); // Start the thread

  }

  // This is the entry point for thread.

  public void run() {

     try {

     for(int i = 15; i > 0; i--) {

        System.out.println(name + ": " + i);

        Thread.sleep(200);

        synchronized(this) {

           while(suspendFlag) {

              wait();

           }

         }

       }

     } catch (InterruptedException e) {

        System.out.println(name + " interrupted.");

     }

     System.out.println(name + " exiting.");

  }

  void mysuspend() {

     suspendFlag = true;

  }

  synchronized void myresume() {

     suspendFlag = false;

      notify();

  }

}

 

class SuspendResume {

  public static void main(String args[]) {

     NewThread ob1 = new NewThread("One");

     NewThread ob2 = new NewThread("Two");

     try {

        Thread.sleep(1000);

        ob1.mysuspend();

        System.out.println("Suspending thread One");

        Thread.sleep(1000);

        ob1.myresume();

        System.out.println("Resuming thread One");

        ob2.mysuspend();

        System.out.println("Suspending thread Two");

        Thread.sleep(1000);

        ob2.myresume();

        System.out.println("Resuming thread Two");

     } catch (InterruptedException e) {

        System.out.println("Main thread Interrupted");

     }

     // wait for threads to finish

     try {

        System.out.println("Waiting for threads to finish.");

        ob1.t.join();

        ob2.t.join();

     } catch (InterruptedException e) {

        System.out.println("Main thread Interrupted");

     }

     System.out.println("Main thread exiting.");

  }

}

Here is the output produced by the above program:

New thread: Thread[One,5,main]

One: 15

New thread: Thread[Two,5,main]

Two: 15

One: 14

Two: 14

One: 13

Two: 13

One: 12

Two: 12

One: 11

Two: 11

Suspending thread One

Two: 10

Two: 9

Two: 8

Two: 7

Two: 6

Resuming thread One

Suspending thread Two

One: 10

One: 9

One: 8

One: 7

One: 6

Resuming thread Two

Waiting for threads to finish.

Two: 5

One: 5

Two: 4

One: 4

Two: 3

One: 3

Two: 2

One: 2

Two: 1

One: 1

Two exiting.

One exiting.

Main thread exiting.



 

The document Multi Threading , Java | Java Programming Basics - Software Development is a part of the Software Development Course Java Programming Basics.
All you need of Software Development at this link: Software Development
9 docs|1 tests

Top Courses for Software Development

FAQs on Multi Threading , Java - Java Programming Basics - Software Development

1. What is multi-threading in Java?
Ans. Multi-threading in Java is a feature that allows multiple threads of execution to run concurrently within a single program. It enables a program to perform multiple tasks at the same time, by dividing the program into smaller units of execution called threads.
2. Why is multi-threading important in Java?
Ans. Multi-threading is important in Java because it can improve the performance and responsiveness of an application. By executing tasks simultaneously, it can utilize the available resources efficiently and reduce the waiting time for certain operations. It is particularly useful in scenarios where multiple tasks need to be performed in parallel, such as handling user interface interactions and background processing.
3. How can multi-threading be implemented in Java?
Ans. Multi-threading can be implemented in Java by using the Thread class or the Runnable interface. The Thread class provides methods to create, start, and control threads, while the Runnable interface represents a task that can be executed concurrently. By extending the Thread class or implementing the Runnable interface, developers can define the code that the thread will execute.
4. What are the advantages of multi-threading in Java?
Ans. The advantages of multi-threading in Java include improved performance, better resource utilization, simplified program design, responsiveness, and the ability to handle concurrent tasks efficiently. It allows for asynchronous execution, which can enhance the user experience and enable smoother execution of complex operations.
5. What are the potential challenges or issues with multi-threading in Java?
Ans. Multi-threading in Java can introduce challenges such as thread synchronization, race conditions, deadlocks, and thread interference. These issues can arise when multiple threads access shared resources concurrently, leading to unexpected behavior or incorrect results. Proper synchronization techniques, such as the use of locks or synchronized blocks, are required to handle these challenges and ensure thread safety.
9 docs|1 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Java | Java Programming Basics - Software Development

,

mock tests for examination

,

Multi Threading

,

Previous Year Questions with Solutions

,

Objective type Questions

,

Multi Threading

,

past year papers

,

Java | Java Programming Basics - Software Development

,

practice quizzes

,

Viva Questions

,

Extra Questions

,

pdf

,

ppt

,

Java | Java Programming Basics - Software Development

,

MCQs

,

shortcuts and tricks

,

video lectures

,

Exam

,

Important questions

,

study material

,

Summary

,

Sample Paper

,

Multi Threading

,

Semester Notes

,

Free

;