Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Thread SynchronizedThreads

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

6 comments so far (post your own) View All Comments Latest 10 Comments:

In this example, programmer is actually creating two different instances of Share class !!...Although we get the output most of the time as given here,code is not properly synchronized.He should have written synchronized(Share.class) instead of synchronized(this).

Posted by Anup on Monday, 06.15.09 @ 09:10am | #88494

Please consider the fact that the above to examples does not show proper synchronization due to the fact that there are two share objects and hence there are two share Object locks. So thread one and thread two can mix up together.

A way to avoid this would be to make the display method "static" and making it a method that would be shared by all instances, and not having their own copy of it

Posted by Lasitha on Monday, 12.22.08 @ 21:18pm | #83063

Please consider the fact that the above to examples does not show proper synchronization due to the fact that there are two share objects and hence there are two share Object locks. So thread one and thread two can mix up together.

A way to avoid this would be to make the display method "static" and making it a method that would be shared by all instances, and not having their own copy of it

Posted by Lasitha on Monday, 12.22.08 @ 21:16pm | #83062

The actual example should be, Here myVar is the lock used by both threads

public class SynThread1 {
public static void main(String[] args) {
String myVar = "abc";
Share t1=new Share(myVar);
t1.start();

Share t2=new Share(myVar);
t2.start();

}
}

class Share extends Thread{
String myVar = null;
static String msg[]={"This", "is", "a", "synchronized", "variable"};
Share(String threadname){
super(threadname);
myVar = threadname;
}
public void run(){
display(getName());
}
public void display(String threadN){
synchronized(myVar){
for(int i=0;i<=4;i++){
System.out.println(threadN+msg[i]);
try{
this.sleep(2000);
}catch(Exception e){}
}
}
}

}

Posted by Maruthi on Wednesday, 04.23.08 @ 18:12pm | #57701

I have a scenario to implement.
we have a clustered environment and we need to synchronize a method acess in business layer of our application. Now in my opinion we will be having two different JVM and two different instance of application running on two different machines so how to synchronize that method access in business layer. we want that at a time only one user can do the processing of that method.

Posted by Raj on Thursday, 04.17.08 @ 15:20pm | #56793

If the share method has a thread.sleep call in it, then the messages overlap:

public class Share extends Thread{

static String msg[]={"This", "is", "a", "synchronized", "variable"};
Share(String threadname){
super(threadname);
}
public void run(){
display(getName());
}
public synchronized void display(String threadN){
for(int i=0;i<=4;i++) {
System.out.println(threadN+msg[i]);
try { this.sleep(100); } catch ( Exception e ) {}
}
try{
this.sleep(1000);
}
catch(Exception e) {}
} // display
}

output:

Thread One: This

Thread Two: This

Thread One: is

Thread Two: is

Thread One: a

Thread Two: a

Thread One: synchronized

Thread Two: synchronized

Thread One: variable

Thread Two: variable


how to prevent this?

Posted by eric givler on Tuesday, 01.15.08 @ 01:06am | #45205

Ask programming questions?

 

 

Back to Tutorial

 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.