i heard, sleep() will lock the current sync method/block
But here, when i call sleep() on thread 1, thread 2 is able to access the same block? Can anyone Explain?
Main.java
public class Main {
public static void main(String args[])
{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
System.out.println("going to start t1");
t1.start();
System.out.println("going to start t2");
t2.start();
}
}
=====================================================================
Thread1.java
public class Thread1 extends Thread{
public void run() {
Syncc s1 = new Syncc();
s1.me("T1:");
}
}
=====================================================================
Thread2.java
public class Thread2 extends Thread{
public void run() {
Syncc s2 = new Syncc();
s2.me("T2:");
}
}
=====================================================================
Syncc.java
public class Syncc{
public void me(String s){
synchronized(this){
for(int i=0; i<=5; i++)
{
System.out.println(s+" "+" "+i);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
==========================================
Output:
going to start t1
going to start t2
T2: 0
T1: 0
T2: 1
T1: 1
T1: 2
T2: 2
T1: 3
T2: 3
T1: 4
T2: 4
T2: 5
T1: 5
BUT according to sleep() method, it should not unlock the current synchronization block right? if so the out put should be..
going to start t1
going to start t2
T1: 0
T1: 1
T1: 2
T1: 3
T1: 4
T1: 5
T2: 0
T2: 1
T2: 2
T2: 3
T2: 4
T2: 5
i mean after thread 1 execution only thread 2 should start right?
Whats the issue?
This is because you have two different instances of Syncc in play here. Each thread has its own copy of Syncc.
Try doing the same with a single instance. You could also synchronize on the static context and try.
To simulate, modify Thread1 and Thread2 to accept an instance of Syncc.
public class Thread1 extends Thread {
private Syncc syncc;
public Thread1(Syncc syncc) {
this.syncc = syncc;
}
public void run() {
this.syncc.me("T1:");
}
}
You can then start them as so:
public static void main(String args[]) {
Syncc syncc = new Syncc();
Thread1 t1 = new Thread1(syncc);
Thread2 t2 = new Thread2(syncc);
System.out.println("going to start t1");
t1.start();
System.out.println("going to start t2");
t2.start();
}
Rules of Sleep, Yield and Join
Sleeping is used to delay execution for a period of time, and no locks are
released when a thread goes to sleep.
A sleeping thread is guaranteed to sleep for at least the time specified in
the argument to the sleep() method (unless it's interrupted), but there is
no guarantee as to when the newly awakened thread will actually return to
running.
The sleep() method is a static method that sleeps the currently executing
thread's state. One thread cannot tell another thread to sleep.
The setPriority() method is used on Thread objects to give threads
a priority of between 1 (low) and 10 (high), although priorities are not
guaranteed, and not all JVMs recognize 10 distinct priority levels—some
levels may be treated as effectively equal.
If not explicitly set, a thread's priority will have the same priority as the
priority of the thread that created it.
The yield() method may cause a running thread to back out if there are
runnable threads of the same priority. There is no guarantee that this will
happen, and there is no guarantee that when the thread backs out there
will be a different thread selected to run. A thread might yield and then
immediately reenter the running state.
The closest thing to a guarantee is that at any given time, when a thread
is running it will usually not have a lower priority than any thread in the
runnable state. If a low-priority thread is running when a high-priority thread
enters runnable, the JVM will usually preempt the running low-priority
thread and put the high-priority thread in.
When one thread calls the join() method of another thread, the currently
running thread will wait until the thread it joins with has completed. Think
of the join() method as saying, "Hey thread, I want to join on to the end
of you. Let me know when you're done, so I can enter the runnable state."
http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060
You have created two Synch objects each object corresponding to one thread . Each object has its own copy of me. Hence when your start each thread, withing the run method the thread is calling its own copy of the function me. Since the two thread act only on their copy this works like a single thread scenario. If you do want to test a multithread scenario then make the method me static(class level method) and apply a class level lock.
Thread1.java
public class Thread1 extends Thread{
public void run() {
Syncc.me("T1:");
}
}
Thread2.java
public class Thread2 extends Thread{
public void run() {
Syncc.me("T2:");
}
}
Syncc.java
public class Syncc{
public static void me(String s){
synchronized(Syncc.class){
for(int i=0; i<=5; i++)
{
System.out.println(s+" "+" "+i);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Related
My code and output are as below :-
My question is that the priority of main thread and thread t4 is 9 and that of thread t is 5 then why line 1 to 4 (marked in output) is coming before line 5 i.e. why t.start() gets priority over t4.start() which will be executed by main thread which has priority of 9.
Main thread priority is 9 so first t4.start() should get executed but then why t.start() is getting executed first ?
And if I reverse the order of calling start method i.e. if I call in order t4.start() and then t.start() then output is as expected.
Output
5
5
Main Thread Priority :9
5
Calling MyRunnable2 from main....9
Child Class - line 1
Child Class - line 2
End of Child loop - line 3
Calling MyRunnable2 from MyThread - line 4
MyRunnable2 Class....Main....9 - line 5
MyRunnable2 Class....From MyThread run....5
CODE :-
public class ThreadPriority
{
public static void main(String[] args)
{
MyThread t = new MyThread();
System.out.println(Thread.currentThread().getPriority());
System.out.println(t.getPriority());
t.setName("From MyThread");
Thread.currentThread().setPriority(9);
System.out.println("Main Thread Priority :" + Thread.currentThread().getPriority());
System.out.println(t.getPriority());
MyRunnable2 r4 = new MyRunnable2();
Thread t4 = new Thread(r4,"Main");
System.out.println("Calling MyRunnable2 from main"+"...."+t4.getPriority());
t.start();
t4.start();
}
}
class MyRunnable2 implements Runnable
{
public void run()
{
System.out.println("MyRunnable2 Class"+"...."+Thread.currentThread().getName()+"...."+Thread.currentThread().getPriority());
}
}
class MyThread extends Thread
{
public void run()
{
for (int i=0;i<2;i++)
System.out.println("Child Class");
System.out.println("End of Child loop");
MyRunnable2 r2 = new MyRunnable2();
Thread t2 = new Thread(r2,"From MyThread run");
System.out.println("Calling MyRunnable2 from MyThread");
t2.start();
}
t4 priority is higher and its doesn't mean t4 will finish first,Furthermore
jvm ask OS for threads and all priorities sets in deep layer of OS,
you are just set a recommendation,it depends on many things!
The higher the integer, the higher the priority.
At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution.
Only when that thread stops, yields, or becomes not runnable for some reason will a lower priority thread start executing.
If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion.
Rule of thumb : At any given time, the highest priority thread is running. However, this is not a guaranteed. The thread scheduler may choose to run a lower priority thread to avoid starvation. For this reason, use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.
I hope the above information clarifies your query.
Here is my following code:
private int counter = 0;
public void incC() {
counter++;
System.out.println("Counter is: "+counter + " "+Thread.currentThread().getName());
}
public void printC() {
System.out.println("Counter is: "+counter + " "+Thread.currentThread().getName());
}
public static void main(String args[]) throws Exception {
Stuff stuff = new Stuff();
Thread t1 = new Thread(() -> {
stuff.incC();
});
Thread t2 = new Thread(() -> {
stuff.printC();
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Here's my output:
Counter is: 1 Thread-1
Counter is: 1 Thread-0
Here t1.join() is called first. So shouldn't thread t2 should wait for thread t1 to die?
How's it possible for thread t2 to output first before t1?
t1.join() makes the current thread wait for thread t1 to die. It has no effects on the timing of any other thread but the current one. You have started two threads and they will run concurrently with no ordering constraints.
I should also note that starting two threads only to have one wait for the other's death defeats the purpose of multithreading.
join() forces to wait current (in your case the main) thread to death of t1, so t2 continues to work
Order for thread execution is not fixed because they work in parallel
Both threads works in parallel and therefore no guarantee on which thread will finish first in your case.
If you want a specific thread to finish first you have to start it and join it with your main thread then start and join the second thread like follows.
t1.start();
t1.join();
t2.start();
t2.join();
Set names for the threads for better understanding.
Also change your question because your threads are working fine.
Here the synchronized block is not working:
class NewThreadt extends Thread {
synchronized void dota(){
for(int i = 5; i > 0; i--)
{
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
}
}
// This is the entry point for the second thread.
public void run() {
dota() ;
System.out.println("Exiting child thread.");
System.out.println(Thread.currentThread()); //gives the name of thread
}
}
class trial {
public static void main(String args[]) {
NewThreadt t=new NewThreadt();
NewThreadt q=new NewThreadt();
t.start();
q.start(); both are in main
System.out.println("Main thread exiting.");
}
}
Output:
Main thread exiting.
5
5
4
4
3
3
2
2
1
1
But when I make the following changes the Synchronized Block works:
class NewThreadt implements Runnable //instead of class NewThreadt extends Thread
NewThreadt abc=new NewThreadt(); //Changes in main()
Thread t=new Thread(abc);
Thread q=new Thread(abc);
t.start();
q.start();
Output:
Main thread exiting.
5
4
3
2
1
5
4
3
2
1
Why is this happening? Aren't both of these examples supposed to work the same?
Synchronization depends on sharing a common lock. The synchronized keyword on an instance method means that for a thread to enter that method it first has to acquire the lock on this.
In your first example there is no shared lock, each of the two threads is locking on itself. Each thread acquires its own lock and nobody blocks, the two threads run concurrently. (The only locking going on here is that when a thread wants to write to the console it has to acquire the console's lock first, so that each println takes place atomically.)
In the second example the same Runnable is passed into each thread, so they both lock on the same object, the Runnable. One thread acquires the lock and executes, the other has to wait for the first thread to release the lock.
If you change the second example to give each thread a separate Runnable:
Thread t=new Thread(new NewThreadt());
Thread q=new Thread(new NewThreadt());
t.start();
q.start();
then you'll see synchronization doesn't work and it behaves like the first example.
I am unable to understand that if my variable is both volatile and static then why threads are not reflecting the common shared value in the output
Output of last few lines is :
Thread is running
4998Thread-0
Thread is running
4999Thread-0
Thread is running
4899Thread-1
Thread is running
public class Test implements Runnable{
volatile static int i=0;
#Override
public void run() {
for(;i<5000;i++)
{ try {
Thread t = Thread.currentThread();
String name = t.getName();
// Thread.sleep(10);
System.out.println(i+name);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Thread is running");
}}
public static void main(String[] args) {
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
// t.run();
t2.start();
}
}
You can't use a compound (multi step) operation like i++ on a volatile variable.
You can have both threads retrieve the value, increase it, and write it back resulting in one lost increment (as you see in your example).
Volatile ensures that changes to the variable are visible to other threads. But it does not ensure synchronization.
From one execution of your program I get this output :
Thread is running
3474Thread-1
Thread is running
3475Thread-0
Thread is running
3477Thread-0
(.... many lines where i goes from 3478 to 4998, with Thread-0 always being the one running...)
Thread is running
4999Thread-0
Thread is running
3476Thread-1
Thread is running
This happens because threads get slices of processor time to be run and their execution can be paused and resumed at any point.
Here Thread-1 is executing line "System.out.println(i+name);" with i having a value of 3476. i+name is evaluated to "3476Thread-1" but just then the Thread-1 execution stops and instead Thread-0 gets its time slice. Thread-0 executes till finalization. And then Thread-1 gets again to execute. We have left it after i+name had been evaluated to "3476Thread-1" and before the call to println. The call is now completed and printed, hence you see "3476Thread-1" at then end. i has been increased to 5000 by Thread-0 but that does not change the result of the evaluation of i+name which was done before all those increases.
The problem is that i++ and i+name are different instructions and thread execution can be paused and resumed between them. To ensure that you get a secuential output you need to ensure than there is no interruption between i++ and i+name. That is, you need to make that set of instructions atomic.
public class Test implements Runnable{
static Object lock = new Object();
volatile static int i=0;
#Override
public void run() {
for(;;)
{
try {
Thread t = Thread.currentThread();
String name = t.getName();
synchronized( lock )
{
if ( i>=5000 )
break;
i++;
System.out.println(i+name);
}
// Thread.sleep(10);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Thread is running");
}
} }
public static void main(String[] args) {
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
// t.run();
t2.start();
}
}
In that program, if Thread-1 gets paused between i++ and i+name it will be inside the a critical section controlled by synchronized(lock). When Thread-0 gets to execute it will reach the synchronized(lock) instruction and will have to stop executing until Thread-1 resumes and gets out of that block. Because the JLS ensures that :
The synchronized statement (§14.19) computes a reference to an object;
it then attempts to perform a lock action on that object's monitor and
does not proceed further until the lock action has successfully
completed. After the lock action has been performed, the body of the
synchronized statement is executed. If execution of the body is ever
completed, either normally or abruptly, an unlock action is
automatically performed on that same monitor.
Here's a code snippet I learned usage of volatile keyword from:
import java.util.concurrent.TimeUnit;
public class VolatileDemo {
private volatile static boolean stop; //remove volatile keyword to see the difference
private static int i = 0;
public static void main(String[] args) throws InterruptedException {
Thread otherThread = new Thread(new Runnable() {
public void run() {
while (!stop)
i++;
}
});
otherThread.start();
TimeUnit.SECONDS.sleep(1);
stop = true;// main thread stops here and should stop otherThread as well
}
}
If you want to observe what volatile does, try to remove it and then follow the execution, this should be obvious after you run those two versions, but basically keyword here prevents java compiler from assuming that stop condition never changes, it will be read every time the condition gets evaluated. Neat, isn't it?
By looking at your code the problem isn't with usage of volatile keyword, the problem is that the expression i++ is not atomic. It actually is 3 step operation:
1) fetch value of i;
2) increment i;
3) save new value of i
When multi-threading comes into play, these might or might not be mixed with other thread's instructions.
So the execution might look like that as well:
1) T1: fetch i;
2) T2: fetch i;
3) T1: increment i;
4) T2: increment i;
5) T1: save i;
6) T2: save i;
If i was 0 you thought you will get 2 as the output, and here's 1 instead.
Go with synchronization, which is pretty simple when not overused and well thought.
Suggested read on synchronization
If two threads are both reading and writing to a shared variable, then using the volatile keyword for that is not enough. You need to use synchronization in that case to guarantee that the reading and writing of the variable is atomic.
Therefore you need to use synchronization when you modify the i value.
There is some weird thing happening. As I enter the synchronized block,I try to print the name of the Thread.After the print statement,I make a husge pause of 100000 seconds.
#Override
public int getNextAvailableVm() {
synchronized(this) {
System.out.println(Thread.currentThread().getName());
try {Thread.sleep(100000000);}catch(Exception exc){}
String dataCenter = dcc.getDataCenterName();
int totalVMs = Temp_Algo_Static_Var.vmCountMap.get(dataCenter);
AlgoHelper ah = (AlgoHelper)Temp_Algo_Static_Var.map.get(dataCenter);
.
.
.
}
}
But as this method is run,name oft the 2 threads are printed.
Thread-11
Thread-13
and it is after this that the long pause occurs. Why is that ? How could the two threads enter the synchronized block,when the first thread has yet not left the block ?
If the two threads are running against the same object then this should not happen.
I would therefore suggest that you are creating a new object for each thread or at least some of the threads are running on different objects.
If you do want multiple objects then you should not use synchronized(this), you should create a static final Object to synchronize on. Please do not sync on this.getClass() as that breaks.
Most likely you are invoking getNextAvailableVm() on different instances of the containing class. Since you are synchronizing on this you will be locking on two different monitors (first thread locks on instance1, second one on instance2).
There are a lot of ways you could correct this:
make the whole method synchronized
synchronize on this.getClass()
define a static object to lock on
use methods from java.util.concurrent.locks to do the locking
These are just some suggestions to address your problem, but to find the right one we would have to know more about your application structure and your requirements.
I guess the below prog, will work like you expected,
Locked on Thread1.Class, Two thread will not execute the method simultaneously
public class Test {
public static void main(String [] args) {
Thread1 t1 = new Thread1();
Thread1 t2 = new Thread1();
t1.start();
t2.start();
}
}
class Thread1 extends Thread{
public void run(){
getNextAvailableVm();
}
public void getNextAvailableVm() {
synchronized(Thread1.class) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
}catch(Exception exc){}
System.out.println(Thread.currentThread().getName());
}
}
}
OUTPUT
Thread-1
Thread-1
Thread-0
Thread-0