Sunteți pe pagina 1din 5

Tuesday, September 22, 2015

Multithreading
1. Thread:
a. worker it will do some job
b. Single unit of a process (task) e.g. Spell Check in Ms Word.
c. In JVM, the main method is invoked by a thread main. GC is also a
thread in JVM (Garbage Collector)
d. Threads are used in client server application.
2. Thread Life Cycle:
a. States: New, Run able(Ready to run)
star
t()

scheduler selects the


thread

Once the job is

Runnin
g

New
Run able
interrupt() , notify(),

dead
sleep() , wait(),

block state (e.g. weather


report)
b. In microseconds, the threads are selected from run able, run and then
pushed back to run able.
c. Thread.yield () makes a running thread to run able.
d. currentThread () returns the current running thread object.
e. priority starts from 1 to 10. 1 min, 5 normal, 10 max priority.
f. e.g.
package com.oracle.threads;
public class MainThreadEx1 {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println(t.getName());
t.setName("Saurav");
System.out.println(t.getName());
System.out.println("Priority: "+t.getPriority());
System.out.println(t);
t.setPriority(10);
System.out.println(t);
}
}
Output:

main
Saurav
Priority: 5
Thread[Saurav,5,main]
Thread[Saurav,10,main]
3. Creating a thread:
a. Two ways
i. extends class with Thread class
ii. by passing a run able object to the thread class constructor.
Runnable<<interface>>
void run();
Runnable<<interface>>
Thread

void run();

MyThread

MyThread t = new MyThread();

MyThread runObj = new


MyThread();
Thread t = new
Thread(runObj);

MyThread
public void
run() {

e.g. package com.oracle.threads;

public class Job1 implements Runnable {


public void run() {
// this method executes automatically OR assigns a job for the thread.
System.out.println("Child Thread running");
}
}

package com.oracle.threads;

public class ThreadEx2 {


public static void main(String[] args) {
System.out.println("Main runing");
Job1 runObj = new Job1();
Thread t = new Thread(runObj);

// main thread creates a thread

t.start(); // runnable state


System.out.println("Main Ends");
}

O/P
Main runing
Main Ends
Child Thread running

Concurrency e.g. --- -class file

*** After t.start() which thread will be called , it cannot be predicted.

**Example:
package com.oracle.threads;

public class MultiThreadEx3 {

public static void main(String[] args) {


Job2 runObj = new Job2();
Thread t1 = new Thread(runObj);
Thread t2 = new Thread(runObj);
t1.start();
t2.start();

}
}
package com.oracle.threads;

public class Job2 implements Runnable {


private int data=10; // goes to heap. local goes to stack.. and will have to
stack memory
public void run() {
data*=2;// this method executes automatically when thread started running OR
assigns a job for the thread.
System.out.println(data);
}
}

O/P:
20 40 or 40 40 or 40 20 because only a single object is present and the data
variable is shared by the two threads.

4. synchronized: keyword is used to lock the object after a thread is inside.

5. sleep and wait block the thread. but sleep method holds the lock and wait
doesnt.
a. Monitor all codes inside sync block.
6. notify() wakes up the sleeping thread.

S-ar putea să vă placă și