Java Multi Thead Programming

662 views
532 views

Published on

What are Threads?
Life Cycle of Thread
Types of Priority
Ways of Multithreading in Java
Example

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
662
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
37
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java Multi Thead Programming

  1. 1. Java Multithreading in Java By Nishant S Mevawala
  2. 2. What are Threads? • A piece of code that run in concurrent with other threads. • Each thread is a statically ordered sequence of instructions. • Threads are being extensively used express concurrency on both single and multiprocessors machines. • Programming a task having multiple threads of control – Multithreading or Multithreaded Programming. 2
  3. 3. Life Cycle of Thread
  4. 4. Threads • Java has built in thread support for Multithreading • Synchronization • Thread Scheduling • Inter-Thread Communication: currentThread yield sleep start run stop setPriority getPriority suspend resume • Java Garbage Collector is a low-priority thread
  5. 5. Types of Priority 1. Thread.MIN_PRIORITY - 1 2. Thread.MAX_PRIORITY - 10 3. Thread.NORM_PRIORITY - 5
  6. 6. Born Stop Start Run Running Yield Sleep Suspended Wait Runnable Stop Resume Wait Notify Stop Dead
  7. 7. Ways of Multithreading in Java • • Create a class that extends the Thread class Create a class that implements the Runnable interface • 1st Method: Extending the Thread class class MyThread extends Thread { public void run() { // thread body of execution • • } } Creating thread: MyThread thr1 = new MyThread(); Start Execution: thr1.start();
  8. 8. Extends Method example class A extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("t From Thread A: i= "+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("t From Thread B: j= "+j); } System.out.println("Exit from B"); } }
  9. 9. class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("t From Thread C: k= "+k); } System.out.println("Exit from C"); } } class ThreadTest { public static void main(String S[]) { new A().start(); new B().start(); new C().start(); } }
  10. 10. 2nd method: Threads by implementing Runnable interface class ClassName implements Runnable { ..... public void run() { // thread body of execution } } • Creating Object: ClassName myObject = new ClassName(); • Creating Thread Object: Thread thr1 = new Thread( myObject ); • Start Execution: thr1.start();
  11. 11. Runnable method Example class ththead implements Runnable { String nm; Thread t; public ththead(String st) { nm=st; t=new Thread(this); } public void run() { for(int i=1;i<=5;i++) { System.out.println(nm + ":" +i); } } }
  12. 12. class theadrun { public static void main(String args[]) { ththead f1=new ththead("one"); ththead f2=new ththead("Two"); ththead f3=new ththead("Three"); f1.t.start(); f2.t.start(); f3.t.start(); } }

×