Java Course
Process -> Execution of a program
Thread -> Single execution sequence with in a process
Each thread will have its own stack memory but single heap per process shared by all threads.
Java multithreading is a process of executing more than one thread simultaneously is known as multithreading in java.
The main purpose of multithreading is to achieve the concurrent execution.
Advantage of Multithreading in Java
-
Save Time
By using multi-threading in java you can perform multiple operation at the same time so it saves time.
-
Doesn't Block Users
Java multithreading doesn't block the user because thread are independent, if exception occurs in one thread it does not affect other thread so you can perform multiple operation at the same time.
Thread
Thread in java is a light weight process and thread is a small part of a process or program.
It is used to achieve multitasking.
Threads have separate path of execution and threads are independent if exception occurs in one thread, it does not affect other threads.Thread share common memory area.
How many ways to create a thread
- Extend the java.lang.Thread class
public class SimpleThread extends Thread { public void run() { System.out.println("Starting " + currentThread().getName()); } public static void main(String args[]) { Thread t1 = new SimpleThread(); t1.setName("t1"); Thread t2 = new SimpleThread(); t2.setName("t2"); t1.start(); t2.start(); } }
Output
Starting t1
Starting t2
Note: this is one possible output. Since t1 and t2 are started independently, the actual order they print in isn't guaranteed - "Starting t2" could appear first instead.
- Implementing the java.lang.Runnable interface
public class RunnableThread implements Runnable { public void run() { System.out.println("Starting " + Thread.currentThread().getName()); } public static void main(String args[]) { RunnableThread rt1 = new RunnableThread(); Thread t1=new Thread(rt1); t1.setName("t1"); RunnableThread rt2 = new RunnableThread(); Thread t2=new Thread(rt2); t2.setName("t2"); t1.start(); t2.start(); } }
Output

- Implement the java.util.concurrent.Callable interface
The executor framework which uses Runnable objects, unfortunately a runnable cannot return a result to the caller.
In case you expect your threads to return a computeable result you can use java.util.concurrent.Callable interface.
public class ThreadSampleCallable implements Callable<String> { private int command; @Override public String call() throws Exception { return "Starting " + command + " " + Thread.currentThread().getName(); } public ThreadSampleCallable(int command) { this.command = command; } public static void main(String args[]) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(5); List<Future<String>> list = new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { Future<String> future = executor.submit(new ThreadSampleCallable(i)); list.add(future); } // now retrieving the result for (Future<String> future : list) { try { System.out.println(future.get()); } catch (InterruptedException ie) { System.out.println(ie); } catch (ExecutionException ee) { System.out.println(ee); } } // This will make the executor accept no new threads and finish all existing // threads in queue executor.shutdown(); // wait until all threads are finish executor.awaitTermination(10, TimeUnit.SECONDS); System.out.println("Finished all threads"); } }
Output

ThreadPool Concept
Java threadpool represents a group of worker thread that are waiting for the job(performed the task) and reuse many times.
A group of fixed size threads are created.
Assigned the job of worker thread by the service provider and complete the job (finish the work) threads contained in the ThreadPool again.
Advantages
- Better performance
- save time
- there is no need to create new thread.