Lecture 9 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar
Multithreading A multithreaded program contains two or more parts that can run concurrently. Each part of such program is called thread. A thread defines the path of execution. Each thread defines a separate path of execution. Multitasking is a specialized form of multithreading Kinds of threads
Introduction A process is actually a program in execution. In process based threads the unit is a program. In thread based threading the unit is a thread. Difference is Big picture vs. Details T1 Process based threads Thread based threads T2 T3 P1 P2 P1 P2 P3
Comparison of Thread Types Processes are heavy weight tasks than threads. IPC is expensive in processes. Context switching is expensive. Threads shares the same address space. Why multithreading? Java Thread Model Event loop with polling approach (single threaded app) In multithreaded applications this approach is eliminated
Life Cycle of a Thread Born Start Ready I/O Completes Quantum expiration Thread dispatch Issue I/O or Synchronized Statement Running Timeout Expires Wait Sleep Sleeping Blocked Waiting Complete
Juggling of a ball And Multithreading How Threads can be Implemented in Java Using Runnable Interface Using Thread Class
A simple program on Threads public class Main { public Main() { } public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println("Current Thread: " + t); t.setName("My Thread"); System.out.println("After name change: " + t); try{ for(int n=5; n > 0; n--) { System.out.println(n); Thread.sleep(1000); } } catch(InterruptedException ex){ } }} A simple program on Threads
Thread implementation using Runnable Interface public class Main implements Runnable { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ } }
public static void main(String[] args) { new Main(); try{ for(int i=5; i > 0; i--){ System.out.println("Main Thread: " + i); Thread.sleep(1000); } catch(InterruptedException ex){ System.out.println("Main Thread Interrupted"); System.out.println("Main Thread Exiting"); } }
Threads extending Threads public class Main extends Thread { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ System.out.println("Child Interrupted"); } System.out.println("Child Thread Exiting"); }
Threads extending Threads public class Main extends Thread { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ System.out.println("Child Interrupted"); } System.out.println("Child Thread Exiting"); }
Creating Multiple Threads public class Main extends Thread { String name; Thread t; public Main(String threadname) { name = threadname; t = new Thread(this,name); System.out.println("New Thread: " + t); t.start(); }
Creating Multiple Threads public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("name: " + x); Thread.sleep(500); } catch(InterruptedException ex){ System.out.println(name + " Interrupted"); System.out.println(name + " Thread Exiting");
Creating Multiple Threads public static void main(String[] args) { new Main("One"); new Main("Two"); new Main("Three"); try{ Thread.sleep(1000); } catch(InterruptedException ex){ System.out.println("Main Thread Interrupted"); System.out.println("Main Thread Exiting");