Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 Process based threads T1 Thread based threads T2 P1 P2 P3 T3 P1 P2 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 Born Life Cycle of a Thread Start Ready Thread dispatch Quantum expiration Timeout Expires Running Wait Waiting I/O Completes Issue I/O or Synchronized Statement Sleep Blocked Sleeping Complete Juggling of a ball And Multithreading How Threads can be Implemented in Java Using Runnable Interface Using Thread Class 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"); } } public class Main extends Thread { Threads extending Threads 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"); } 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 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"); } }