Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

When we lock an Object, it acquires lock on all the fields of the Object. Can u please explain me this sentence… the way i understood is ,If i have a object A and object B is a field of Object A.It aquires lock on object B also… Is that correct
- Naveen J
Quote: ________________________________________________________________________________________________________ public class MyObject { //locks on the class object’s monitor public static synchronized void doSomething() { // … } } // hackers code synchronized (MyObject.class) { while (true) { Thread.sleep(Integer.MAX_VALUE); // Indefinitely delay MyObject } } Notice that hacker code is getting lock on class monitor and not releasing it, it will cause deadlock and DoS in the system. ______________________________________________________________________________________________________ This may not really correct. Please see the code below: _________________________________________________________________________________________________ public class Hack { public static void main(String[] args) throws Exception{ new Thread(new R1()).start(); Thread.sleep(1000); new Thread(new R2()).start(); new Thread(new R3()).start(); } } class MyObject { public void sayHello(){ synchronized(MyObject.class){ System.out.println(“C: Hello, Word”); } } public synchronized void sayHello2(){ System.out.println(“M: Hello, Word”); } } class R1 implements Runnable{ @Override public void run(){ synchronized (MyObject.class) { System.out.println(“Lock class instance!”); while (true) { try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } } } } } class R2 implements Runnable{ @Override public void run() { MyObject myObject = new MyObject(); myObject.sayHello(); } } class R3 implements Runnable{ @Override public void run() { MyObject myObject = new MyObject(); myObject.sayHello2(); } } ___________________________________________________________________________________________________ Result: ______________________________ Lock class instance! M: Hello, Word ______________________________ see more: https://stackoverflow.com/a/2056256/2893073 - Thanks!
- Eddy
package com.journaldev.threads; public class ThreadSafety { public static void main(String[] args) throws InterruptedException { ProcessingThread pt = new ProcessingThread(); Thread t1 = new Thread(pt, “t1”); t1.start(); Thread t2 = new Thread(pt, “t2”); t2.start(); //wait for threads to finish processing t1.join(); t2.join(); System.out.println(“Processing count=”+pt.getCount()); } } class ProcessingThread implements Runnable{ private int count; @Override public void run() { for(int i=1; i < 5; i++){ processSomething(i); count++; } } public int getCount() { return this.count; } private void processSomething(int i) { // processing some job try { Thread.sleep(i*1000); } catch (InterruptedException e) { e.printStackTrace(); } } } When I test the code with Idea tools, the result is always equal to 8. Can you tell me why?
- jeesk
Hi, I tried example SyncronizedMethod several times. It doesn’t corrupt the String Array and I am geting the o/p [1:t1:t3:t2, 2:t1:t3:t2, 3:t1:t3:t2, 4:t1:t3:t2, 5:t1:t3:t2, 6:t1:t3:t2] evry time I run the program. I don’t understand the reason. Doesn’t seems need of using synchronization.
- Namita Rode