• Like
Threads in java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Threads in java

  • 1,889 views
Published

threads in java

threads in java

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,889
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
118
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. Threads in Java Programming Mukesh Singh 5 th cse
  • 2. What are threads?
    • * Each thread is like an operating system process, except that all threads in a process share the memory.
    • * Threads execute code concurrently and therefore must carefully control access to shared resources.
      • Otherwise data can become inconsistent.
  • 3. Creating a Thread in Java
    • public class MyThread extends Thread {
    • public MyThread(String threadName) {
    • super(threadName);
    • }
    • public void run () {
    • for(int i = 0; i < 10; i++) {
    • System.out.println(i + “ “ + getName ());
    • try {
    • sleep ((long)(Math.random() * 1000));
    • } catch(InterruptedException e) {}
    • }
    • }
    • }
  • 4. Creating a Thread in Java
    • public class ThreadTest {
    • public static void main(String[] args) {
    • for(int i = 0; i < args.length; i++) {
    • MyThread t = new MyThread(args[i]);
    • t.start ();
    • }
    • }
    • }
    • o/p---------
    • > java ThreadTest max payne
    • 0 max
    • 1 payne
    • 2 max
    • 3 max
    • 4 payne
    • 5 max
    • 6 payne
    • 7 payne
    • ...
  • 5. Thread Lifecycle Born Blocked Runnable Dead stop() start() stop() Active block on I/O I/O available JVM sleep(500) wake up suspend() resume() wait notify
  • 6. Thread Priority
    • * Every thread has a priority
    • * When a thread is created, it inherits the priority of the thread that created it
    • * The priority values range from 1 to 10, in increasing priority
  • 7. Thread Priority (cont.)
    • * The priority can be adjusted subsequently using the setPriority() method
    • * The priority of a thread may be obtained using getPriority()
    • * Priority constants are defined:
      • MIN_PRIORITY=1
      • MAX_PRIORITY=10
      • NORM_PRIORITY=5
    The main thread is created with priority NORM_PRIORITY
  • 8. Thread Synchronization
    • * Protects access to code, not to data
      • Make data members private
      • Synchronize accessor methods
    • * Puts a “force field” around the locked object so no other threads can enter
        • Actually, it only blocks access to other synchronizing threads…
        • Ex.- producer – consumer problem.
  • 9. Consumer
    • * Consumer:
      • synchronized (lock) {
      • while (!resourceAvailable()) {
      • lock.wait();
      • }
      • consumeResource();
      • }
  • 10. Producer
    • * Producer:
      • produceResource();
      • synchronized (lock) {
      • lock.notifyAll();
      • }
  • 11. Wait/Notify Sequence Lock Object Consumer Thread Producer Thread 1. synchronized(lock){ 2. lock.wait(); 3. produceResource() 4. synchronized(lock) { 5. lock.notify(); 6.} 7. Reacquire lock 8. Return from wait() 9. consumeResource(); 10. }
  • 12. Thread Deadlock
    • * If two threads are competing for more than one lock
    • * Example: bank transfer between two accounts
      • Thread A has a lock on account 1 and wants to lock account 2
      • Thread B has a lock on account 2 and wants to lock account 1
  • 13. Avoiding Deadlock
    • * No universal solution
    • * Create new threads
    • * Check and back off
    • * Timeout
    • * Minimize or remove synchronization
  • 14. Java is Multithreaded
    • * Multithreaded applications allow several processes to execute simultaneously
    • * Several clients can be handled simultaneously
  • 15. Example of multithreading
  • 16.  
  • 17. Thanks