why Synchronized method allowing multiple thread to run concurrently?












8














I have following program in same file. I have synchronized the run() method.



class MyThread2 implements Runnable {
Thread t;

MyThread2(String s) {
t=new Thread(this,s);
t.start();
}

public synchronized void run() {
for (int i=0;i<3;i++) {
System.out.println("Thread name : "+ Thread.currentThread).getName());
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.getMessage();
}
}
}
}

class TestSync {
public static void main(String args) {
MyThread2 m1=new MyThread2("My Thread 1");
c.fun();
}
}

class c {
static void fun() {
MyThread2 m1=new MyThread2("My Thread 4");
}
}


output is



Thread name : My Thread 1
Thread name : My Thread 4
Thread name : My Thread 4
Thread name : My Thread 1
Thread name : My Thread 1
Thread name : My Thread 4


My question is why is synchronized method allowing both "My Thread 1" and "My Thread 4" thread access concurrently?










share|improve this question




















  • 2




    The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
    – Mat
    Oct 5 '11 at 10:04










  • The misconception is that you are locking a method, instead you have a method which locks the instance.
    – Peter Lawrey
    Oct 5 '11 at 10:14
















8














I have following program in same file. I have synchronized the run() method.



class MyThread2 implements Runnable {
Thread t;

MyThread2(String s) {
t=new Thread(this,s);
t.start();
}

public synchronized void run() {
for (int i=0;i<3;i++) {
System.out.println("Thread name : "+ Thread.currentThread).getName());
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.getMessage();
}
}
}
}

class TestSync {
public static void main(String args) {
MyThread2 m1=new MyThread2("My Thread 1");
c.fun();
}
}

class c {
static void fun() {
MyThread2 m1=new MyThread2("My Thread 4");
}
}


output is



Thread name : My Thread 1
Thread name : My Thread 4
Thread name : My Thread 4
Thread name : My Thread 1
Thread name : My Thread 1
Thread name : My Thread 4


My question is why is synchronized method allowing both "My Thread 1" and "My Thread 4" thread access concurrently?










share|improve this question




















  • 2




    The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
    – Mat
    Oct 5 '11 at 10:04










  • The misconception is that you are locking a method, instead you have a method which locks the instance.
    – Peter Lawrey
    Oct 5 '11 at 10:14














8












8








8


3





I have following program in same file. I have synchronized the run() method.



class MyThread2 implements Runnable {
Thread t;

MyThread2(String s) {
t=new Thread(this,s);
t.start();
}

public synchronized void run() {
for (int i=0;i<3;i++) {
System.out.println("Thread name : "+ Thread.currentThread).getName());
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.getMessage();
}
}
}
}

class TestSync {
public static void main(String args) {
MyThread2 m1=new MyThread2("My Thread 1");
c.fun();
}
}

class c {
static void fun() {
MyThread2 m1=new MyThread2("My Thread 4");
}
}


output is



Thread name : My Thread 1
Thread name : My Thread 4
Thread name : My Thread 4
Thread name : My Thread 1
Thread name : My Thread 1
Thread name : My Thread 4


My question is why is synchronized method allowing both "My Thread 1" and "My Thread 4" thread access concurrently?










share|improve this question















I have following program in same file. I have synchronized the run() method.



class MyThread2 implements Runnable {
Thread t;

MyThread2(String s) {
t=new Thread(this,s);
t.start();
}

public synchronized void run() {
for (int i=0;i<3;i++) {
System.out.println("Thread name : "+ Thread.currentThread).getName());
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.getMessage();
}
}
}
}

class TestSync {
public static void main(String args) {
MyThread2 m1=new MyThread2("My Thread 1");
c.fun();
}
}

class c {
static void fun() {
MyThread2 m1=new MyThread2("My Thread 4");
}
}


output is



Thread name : My Thread 1
Thread name : My Thread 4
Thread name : My Thread 4
Thread name : My Thread 1
Thread name : My Thread 1
Thread name : My Thread 4


My question is why is synchronized method allowing both "My Thread 1" and "My Thread 4" thread access concurrently?







java multithreading synchronized






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '11 at 10:11









Guillaume

17.5k144565




17.5k144565










asked Oct 5 '11 at 10:01









user980089

152110




152110








  • 2




    The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
    – Mat
    Oct 5 '11 at 10:04










  • The misconception is that you are locking a method, instead you have a method which locks the instance.
    – Peter Lawrey
    Oct 5 '11 at 10:14














  • 2




    The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
    – Mat
    Oct 5 '11 at 10:04










  • The misconception is that you are locking a method, instead you have a method which locks the instance.
    – Peter Lawrey
    Oct 5 '11 at 10:14








2




2




The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
– Mat
Oct 5 '11 at 10:04




The code you posted has at least one syntax error, and is really hard to read as is. Make sure you post your actual code and try to format it legibly.
– Mat
Oct 5 '11 at 10:04












The misconception is that you are locking a method, instead you have a method which locks the instance.
– Peter Lawrey
Oct 5 '11 at 10:14




The misconception is that you are locking a method, instead you have a method which locks the instance.
– Peter Lawrey
Oct 5 '11 at 10:14












1 Answer
1






active

oldest

votes


















30














synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).



Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.



If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:



class MyThread2 implements Runnable {
private static final Object lock = new Object();
...
public void run() {
synchronized(lock) {
...
}
}
}





share|improve this answer























  • Thanks for the answer
    – user980089
    Oct 6 '11 at 16:19










  • If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
    – Sajad
    Feb 15 '14 at 22:04












  • @ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
    – NPE
    Feb 15 '14 at 22:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7659615%2fwhy-synchronized-method-allowing-multiple-thread-to-run-concurrently%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









30














synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).



Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.



If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:



class MyThread2 implements Runnable {
private static final Object lock = new Object();
...
public void run() {
synchronized(lock) {
...
}
}
}





share|improve this answer























  • Thanks for the answer
    – user980089
    Oct 6 '11 at 16:19










  • If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
    – Sajad
    Feb 15 '14 at 22:04












  • @ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
    – NPE
    Feb 15 '14 at 22:06
















30














synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).



Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.



If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:



class MyThread2 implements Runnable {
private static final Object lock = new Object();
...
public void run() {
synchronized(lock) {
...
}
}
}





share|improve this answer























  • Thanks for the answer
    – user980089
    Oct 6 '11 at 16:19










  • If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
    – Sajad
    Feb 15 '14 at 22:04












  • @ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
    – NPE
    Feb 15 '14 at 22:06














30












30








30






synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).



Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.



If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:



class MyThread2 implements Runnable {
private static final Object lock = new Object();
...
public void run() {
synchronized(lock) {
...
}
}
}





share|improve this answer














synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).



Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.



If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:



class MyThread2 implements Runnable {
private static final Object lock = new Object();
...
public void run() {
synchronized(lock) {
...
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 5 '11 at 10:30

























answered Oct 5 '11 at 10:05









NPE

347k60741873




347k60741873












  • Thanks for the answer
    – user980089
    Oct 6 '11 at 16:19










  • If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
    – Sajad
    Feb 15 '14 at 22:04












  • @ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
    – NPE
    Feb 15 '14 at 22:06


















  • Thanks for the answer
    – user980089
    Oct 6 '11 at 16:19










  • If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
    – Sajad
    Feb 15 '14 at 22:04












  • @ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
    – NPE
    Feb 15 '14 at 22:06
















Thanks for the answer
– user980089
Oct 6 '11 at 16:19




Thanks for the answer
– user980089
Oct 6 '11 at 16:19












If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
– Sajad
Feb 15 '14 at 22:04






If we have only one instance of class and multiple threads, is there possible to run some synchronized methods concurrently?
– Sajad
Feb 15 '14 at 22:04














@ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
– NPE
Feb 15 '14 at 22:06




@ssss: Sure: static vs non-static, using synchronized blocks with different locks etc. If you need further information, please post a new question.
– NPE
Feb 15 '14 at 22:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7659615%2fwhy-synchronized-method-allowing-multiple-thread-to-run-concurrently%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'