Threads

38
Threads

description

This Presentation has the details about the Threads in JAVA

Transcript of Threads

Threads

ThreadsJava ThreadsA thread is not an objectA thread is a flow of controlA thread is a series of executed statementsA thread is a nested sequence of method callsThe Thread ObjectA thread is not an objectA Thread is an object

void start()Creates a new thread and makes it runnable

void run()The new thread begins its life inside this methodThread Creation DiagramThread t = new BThread();

t.start();

doMoreStuff();Object ABThread() {}

void start() {// create thread}

void run() {doSomething();}Object BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Thread t = new BThread();

t.start();

doMoreStuff();BThread() {}

void start() {// create thread}

void run() {doSomething();}Thread Creation DiagramObject AObject BThread (extends Thread) Runnable InterfaceA helper to the thread objectThe Thread objects run() method calls the Runnable objects run() methodAllows threads to run inside any object, regardless of inheritanceRunnable ExampleTalker talker = new Talker();Thread t = new Thread(talker);t.Start();---class Talker implements Runnable { public void run() { while (true) { System.out.println(yakitty yak); } }}Thread LifecycleBornBlockedRunnableDeadstop()start()stop()Activeblock on I/OI/O availableJVMsleep(500)wake upsuspend()resume()waitnotifyBlocking ThreadsWhen reading from a stream, if input is not available, the thread will blockThread is suspended (blocked) until I/O is availableAllows other threads to automatically activateWhen I/O available, thread wakes back up againBecomes runnableNot to be confused with the Runnable interfaceThread SchedulingIn general, the runnable thread with the highest priority is active (running)Java is priority-preemptiveIf a high-priority thread wakes up, and a low-priority thread is runningThen the high-priority thread gets to run immediatelyAllows on-demand processingEfficient use of CPUThread StarvationIf a high priority thread never blocksThen all other threads will starveMust be clever about thread priorityThread Priorities: General StrategiesThreads that have more to do should get lower priorityCounterintuitiveCut to head of line for short tasksGive your I/O-bound threads high priorityWake up, immediately process data, go back to waiting for I/OThread SynchronizationLanguage keyword: synchronizedTakes out a monitor lock on an objectExclusive lock for that threadIf lock is currently unavailable, thread will blockThread SynchronizationProtects access to code, not to dataMake data members privateSynchronize accessor methodsPuts a force field around the locked object so no other threads can enterActually, it only blocks access to other synchronizing threadsWait and NotifyAllows two threads to cooperateBased on a single shared lock objectWait and Notify: CodeConsumer:synchronized (lock) { while (!resourceAvailable()) { lock.wait(); } consumeResource();}Wait and Notify: CodeProducer:produceResource();synchronized (lock) { lock.notifyAll();}Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Lock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify SequenceLock ObjectConsumerThreadProducerThread1. synchronized(lock){2. lock.wait();3. produceResource()4. synchronized(lock) {5. lock.notify();6.}7. Reacquire lock8. Return from wait()9. consumeResource();10. }Wait/Notify Sequence35Wait/Notify: DetailsOften the lock object is the resource itselfSometimes the lock object is the producer thread itselfWait/Notify Example: Blocking Queueclass BlockingQueue extends Queue { public synchronized Object remove() { while (isEmpty()) { wait(); // really this.wait() } return super.remove(); } public synchronized void add(Object o) { super.add(o); notifyAll(); // this.notifyAll() }}The End.. Thank You ..