Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

what's wrong with my code?about synchronized,please help me.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Thread1 extends Thread{
int threadNumber;
Thread1(int xx){
threadNumber=xx;
}
public static void main(String[]args){
Thread1 obj1=new Thread1(1);
Thread1 obj2=new Thread1(2);
obj1.start();
obj2.start();
System.out.println("Main ends");}
public void run(){
synchronized(this){
//why outcome can't synchronized?
for(int i=10;i>0;i--){
System.out.println("Thread = "+threadNumber);
}
}
}
}
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your program creates two objects that run independently on separate threads, each has it own field "threadNumber".
Do you understand what the synchronized keyword does? It is suppose to sync access to shared resources, in your code there is nothing shared that need to be synchronized!
It's a good idea to always state what you are trying to achieve, otherwise no one can help you! Simple asking what's wrong is not helpful
 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OH,my god~~~
i made a big mistake,i see you mean~~thanks
a lot Rajinder Yadav ;I will study and think hard by myself .
you know i just study java three month,now preparing SCJP
ok~~thanks again
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threading can be a very difficult concept to master, I know when I started it took me a very long time.
Three months may or may not be a very long time depending on your background! I have been playing around with java from about a month now, off-and-on, so you're ahead of me by two months
But keep at it and have fun!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic