Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Threads in java


Published on

threads in java

Published in: Education
  • Be the first to comment

Threads in java

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