Showing posts with label creating multiple threads in java. Show all posts
Showing posts with label creating multiple threads in java. Show all posts

Thursday, 29 August 2019

Multithreading in java | Creating multiple threads in java - Online Help

Multithreading in Java:

Let's discuss what is multithreading in java?

Java provides built-in support for the multi-threaded program. A multi-threaded 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. Thus multithreading is the specialized form of multitasking.

Multithreading enables you to write very efficient programs that make maximum use of the CPU because idle time can be kept to a minimum. This is especially important for the interactive, networked environment in which Java operates because idle time is common.

A program can be divided into a number of small processes. Each small process can be addressed as a single thread(lightweight process). A multithreaded program contains two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing a spelling check.

To create a new thread, your program with either extends Thread or implement the Runnable interface. To know, how to create a thread and what are the ways are available to create a thread and what is the life cycle of the thread, then read this below article:

Creating a thread in java 



Advantages of multithreading: 

  •  multithreading reduces computation time. because it runs more than one thread at a time.
  •  It improves the performance of an application.
  •  Threads distribute the same address space it saves the memory space.
  •  Context switching between threads is usually less costly than between processes.
  •  The cost of communication between Threads is comparatively low.


Creating multiple threads in java:

so far we have seen using only two threads: the main thread and the one-child thread. however, your program can spawn as many threads as its needs. For example, the following program creates three child threads. The main thread starts the work of all of its child thread, context switching will be done between the threads.


//Create multiple threads
Class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println(“new thread:”+t);
 t.start();          //start the thread
}

//This is the entry point of the thread
public void run()
{
try
{
for(int i=5;i>0;i++)
{
System.out.println(name+”:”+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+”existing”);
}
}
Class multithreaddemo
{
Public static void main(String agrs[])
{
newThread(“one”);//start thread
new NewThread(“two”);
new NewThread(“three”);
try
{
// wait for the other thread to end
Thread.sleep(10000);
}
catch(interruptedException e)
{
System.out.println(“main thread interrupted”);
}
System.out.println(“main thread existing”);
}
}

ALSO READ:
Exception handling in java



The Output from this program is shown here:

new thread: thread[ one,5, main]
new thread: thread[ two,5, main]
new thread: thread[ three,5, main]
one:5
two:5
three:5
one:4
two:4
Three:4
one:3
two:3
Three:3
one:2
two:2
Three:2
one:1
two:1
three:1
one existing
two existing
three existing
main thread existing

Major concept of java multithreading:

while working with the java multithreading program, you should know about the following factor:
  • synchronization of thread.
  • how to handle inter-thread communication.
  • what is a deadlock and how to handle it?