blog: http://tunatore.wordpress.com
https://www.udemy.com/spring-framework-4-course-and-core-spring-certification/?couponCode=spring39coupon
Core Spring Certified Professional + SCJP 6.0 + SCWCD 1.5
Software development is an exercise in thinking not coding.
SCJP5 and SCWCD1.5
Think Twice Act Wise...
but here 2nd thread is not accessing the non-synchronized method
Preparing for SCJP 6.0
public class SynchronizeTest implements Runnable
{
public static void main(String[] args)
{
SynchronizeTest obj = new SynchronizeTest();
Thread t1 = new Thread(obj);
t1.start();
obj.non_sync();
SynchronizeTest obj2 = new SynchronizeTest();
obj2.non_sync();
}
public void run()
{
sync();
}
public synchronized void sync()
{
System.out.println("Sync: " + Thread.currentThread().getName());
non_sync();
}
public void non_sync()
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
}
System.out.println("Non-Sync: " + Thread.currentThread().getName());
}
}
Preparing for SCJP 6.0
as this makes no sense to me. Nor am I sure what your code is attempting to show. However, I played about with it a bit and by adding a print statement on entry and exit to the non_sync method it is clear that with both the main thread and the user thread t1 call the method non-sequentially.it acquires a lock on the non-synchronized code as well
SCJP5 and SCWCD1.5
Think Twice Act Wise...
Preparing for SCJP 6.0
blog: http://tunatore.wordpress.com
https://www.udemy.com/spring-framework-4-course-and-core-spring-certification/?couponCode=spring39coupon
Core Spring Certified Professional + SCJP 6.0 + SCWCD 1.5
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |