Your SlideShare is downloading. ×
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Java Multi Thead Programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java Multi Thead Programming

320

Published on

What are Threads? …

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
320
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java Multithreading in Java By Nishant S Mevawala
  • 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. Life Cycle of Thread
  • 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. Types of Priority 1. Thread.MIN_PRIORITY - 1 2. Thread.MAX_PRIORITY - 10 3. Thread.NORM_PRIORITY - 5
  • 6. Born Stop Start Run Running Yield Sleep Suspended Wait Runnable Stop Resume Wait Notify Stop Dead
  • 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. 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. 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. 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. 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. 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(); } }

×