Skip to main content
spelling, make some sense out of the sentences
Source Link

when iWhen I try to execute the piece of icode, I am getting output like 1222112121212122121212221121212121221212. here when iWhen I declare the method is declared as synchronizedsynchronized, it should not allow the other thread to take over the control

package com.test; public class TestProject extends Thread { public void run() { synchronized (this) { for (int i = 0; i < 10; i++) { System.out.print(getName()); } } } public static void main(String[] args) throws InterruptedException { TestProject myProject = new TestProject(); TestProject myProject1 = new TestProject(); myProject.setName("1"); myProject1.setName("2"); myProject.start(); myProject1myProacject1.start(); } } 

so toSo, how do I alter the code to get a output of 11111111112222222222 or 22222222221111111111 in sequential format?

when i try to execute the piece of i am getting output like 12221121212121221212. here when i method is declared as synchronized it should not allow the other thread to take over the control

package com.test; public class TestProject extends Thread { public void run() { synchronized (this) { for (int i = 0; i < 10; i++) { System.out.print(getName()); } } } public static void main(String[] args) throws InterruptedException { TestProject myProject = new TestProject(); TestProject myProject1 = new TestProject(); myProject.setName("1"); myProject1.setName("2"); myProject.start(); myProject1.start(); } } 

so to alter the code to get a output of 11111111112222222222 or 22222222221111111111 in sequential format?

When I try to execute the piece of code, I am getting output like 12221121212121221212. When I declare the method as synchronized, it should not allow the other thread to take over the control

package com.test; public class TestProject extends Thread { public void run() { synchronized (this) { for (int i = 0; i < 10; i++) { System.out.print(getName()); } } } public static void main(String[] args) throws InterruptedException { TestProject myProject = new TestProject(); TestProject myProject1 = new TestProject(); myProject.setName("1"); myProject1.setName("2"); myProject.start(); myProacject1.start(); } } 

So, how do I alter the code to get a output of 11111111112222222222 or 22222222221111111111 in sequential format?

Source Link
User2364902
  • 431
  • 2
  • 4
  • 15

synchronized in thread concepts of java

when i try to execute the piece of i am getting output like 12221121212121221212. here when i method is declared as synchronized it should not allow the other thread to take over the control

package com.test; public class TestProject extends Thread { public void run() { synchronized (this) { for (int i = 0; i < 10; i++) { System.out.print(getName()); } } } public static void main(String[] args) throws InterruptedException { TestProject myProject = new TestProject(); TestProject myProject1 = new TestProject(); myProject.setName("1"); myProject1.setName("2"); myProject.start(); myProject1.start(); } } 

so to alter the code to get a output of 11111111112222222222 or 22222222221111111111 in sequential format?