Lets create different thread and put them into different states. And see what Jstack command output gives.
----------------------------------------------------------------------------------------------------------------
public class ThreadRunner {
public static void main(String[] args) {
// NEW, RUNNABLE,
Thread runningJob = new Thread(new RunningJob(), "RunningJob");
runningJob.start();
// BLOCKED,
Account account = new Account();
Thread blockedJob1 = new Thread(new BlockedJob(account), "blockedJob1");
Thread blockedJob2 = new Thread(new BlockedJob(account), "blockedJob2");
blockedJob1.start();
blockedJob2.start();
// WAITING,
PriorityQueue<Sheep> priorityQueue1 = new PriorityQueue<Sheep>();
Thread waitingJob = new Thread(new WaitingJob(priorityQueue1), "waitingJob");
waitingJob.start();
// TIMED_WAITING,
PriorityQueue<Sheep> priorityQueue2 = new PriorityQueue<Sheep>();
Thread timedWaintingJob = new Thread(new TimedWaitingJob(priorityQueue2), "timedWaintingJob");
timedWaintingJob.start();
// SLEEPING JOB
Thread sleepingJob = new Thread(new SleepingJob(), "sleepingJob");
sleepingJob.start();
}
}
---------------------------------------------------------------------------------------------------------------
public class RunningJob implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + " Runing ");
}
}
}
-----------------------------------------------------------------------------------------------------------------
public class BlockedJob implements Runnable {
private Account account;
public BlockedJob(Account account) {
this.account = account;
}
public void run() {
System.out.println("BlockedJob is going to block " + Thread.currentThread().getName());
synchronized (account) {
while (true) {
System.out.println("BlockedJob inside synchronized block " + Thread.currentThread().getName());
}
}
}
}
----------------------------------------------------------------------------------------------------------------
public class ThreadRunner {
public static void main(String[] args) {
// NEW, RUNNABLE,
Thread runningJob = new Thread(new RunningJob(), "RunningJob");
runningJob.start();
// BLOCKED,
Account account = new Account();
Thread blockedJob1 = new Thread(new BlockedJob(account), "blockedJob1");
Thread blockedJob2 = new Thread(new BlockedJob(account), "blockedJob2");
blockedJob1.start();
blockedJob2.start();
// WAITING,
PriorityQueue<Sheep> priorityQueue1 = new PriorityQueue<Sheep>();
Thread waitingJob = new Thread(new WaitingJob(priorityQueue1), "waitingJob");
waitingJob.start();
// TIMED_WAITING,
PriorityQueue<Sheep> priorityQueue2 = new PriorityQueue<Sheep>();
Thread timedWaintingJob = new Thread(new TimedWaitingJob(priorityQueue2), "timedWaintingJob");
timedWaintingJob.start();
// SLEEPING JOB
Thread sleepingJob = new Thread(new SleepingJob(), "sleepingJob");
sleepingJob.start();
}
}
---------------------------------------------------------------------------------------------------------------
public class RunningJob implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + " Runing ");
}
}
}
-----------------------------------------------------------------------------------------------------------------
public class BlockedJob implements Runnable {
private Account account;
public BlockedJob(Account account) {
this.account = account;
}
public void run() {
System.out.println("BlockedJob is going to block " + Thread.currentThread().getName());
synchronized (account) {
while (true) {
System.out.println("BlockedJob inside synchronized block " + Thread.currentThread().getName());
}
}
}
}
-------------------------------------------------------------------------------------------------------------------
public class WaitingJob implements Runnable {
private Queue<Sheep> queue;
public WaitingJob(Queue<Sheep> queue) {
this.queue = queue;
}
public void run() {
synchronized (queue) {
if(queue.isEmpty()){
System.out.println("WaitingJob is going to wait");
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
-----------------------------------------------------------------------------------------------------------------
public class TimedWaitingJob implements Runnable {
private Queue<Sheep> queue;
public TimedWaitingJob(Queue<Sheep> queue) {
this.queue = queue;
}
public void run() {
synchronized (queue) {
if (queue.isEmpty()) {
System.out.println("TimedWaitingJob is going to wait");
try {
queue.wait(5 * 1000 * 60 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
------------------------------------------------------------------------------------------------------------------
public class SleepingJob implements Runnable {
public void run() {
while(true){
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
--------------------------------------------------------------------------------------------------------------------
arham@arham-Lenovo-G510:~$ jstack 3611
"sleepingJob" prio=10 tid=0x00007f90640b9000 nid=0xe33 waiting on condition [0x00007f9058578000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.thread.jobs.SleepingJob.run(SleepingJob.java:8)
at java.lang.Thread.run(Thread.java:745)
"timedWaintingJob" prio=10 tid=0x00007f90640b7000 nid=0xe32 in Object.wait() [0x00007f9058679000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000c6006898> (a java.util.PriorityQueue)
at com.thread.jobs.TimedWaitingJob.run(TimedWaitingJob.java:18)
- locked <0x00000000c6006898> (a java.util.PriorityQueue)
at java.lang.Thread.run(Thread.java:745)
"waitingJob" prio=10 tid=0x00007f90640b5000 nid=0xe31 in Object.wait() [0x00007f905877a000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000c600a068> (a java.util.PriorityQueue)
at java.lang.Object.wait(Object.java:503)
at com.thread.jobs.WaitingJob.run(WaitingJob.java:17)
- locked <0x00000000c600a068> (a java.util.PriorityQueue)
at java.lang.Thread.run(Thread.java:745)
"blockedJob2" prio=10 tid=0x00007f90640b2000 nid=0xe30 waiting for monitor entry [0x00007f905887b000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.thread.jobs.BlockedJob.run(BlockedJob.java:16)
- waiting to lock <0x00000000c600c068> (a com.thread.dto.Account)
at java.lang.Thread.run(Thread.java:745)
"blockedJob1" prio=10 tid=0x00007f90640b0000 nid=0xe2f runnable [0x00007f905897c000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:345)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
- locked <0x00000000c600a228> (a java.io.BufferedOutputStream)
at java.io.PrintStream.write(PrintStream.java:482)
- locked <0x00000000c600a100> (a java.io.PrintStream)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
- locked <0x00000000c6006960> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.write(PrintStream.java:527)
- eliminated <0x00000000c600a100> (a java.io.PrintStream)
at java.io.PrintStream.print(PrintStream.java:669)
at java.io.PrintStream.println(PrintStream.java:806)
- locked <0x00000000c600a100> (a java.io.PrintStream)
at com.thread.jobs.BlockedJob.run(BlockedJob.java:16)
- locked <0x00000000c600c068> (a com.thread.dto.Account)
at java.lang.Thread.run(Thread.java:745)
"RunningJob" prio=10 tid=0x00007f90640ae800 nid=0xe2e waiting for monitor entry [0x00007f9058a7d000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.io.PrintStream.println(PrintStream.java:805)
- waiting to lock <0x00000000c600a100> (a java.io.PrintStream)
at com.thread.jobs.RunningJob.run(RunningJob.java:7)
at java.lang.Thread.run(Thread.java:745)
---------------------------------------------------------------------------------------------------------------
Output:- Every thing can be understand easily except RunningJob is in BLOCKED state cause it is blocked on object monitor which is PrintStream out. Println method is synchronized. So if any other thread is writing then all other thread will be blocked.
No comments:
Post a Comment