Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Object Oriented Programming Using Java

Similar presentations


Presentation on theme: "Lecture 9 Object Oriented Programming Using Java"— Presentation transcript:

1 Lecture 9 Object Oriented Programming Using Java
By Rashid Ahmad Department of Computer Science University of Peshawar

2 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

3 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

4 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

5 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

6 Juggling of a ball And Multithreading How Threads can be Implemented in Java Using Runnable Interface Using Thread Class

7 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

8 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){ } }

9 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"); } }

10 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"); }

11 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"); }

12 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(); }

13 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");

14 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");


Download ppt "Lecture 9 Object Oriented Programming Using Java"

Similar presentations


Ads by Google