Home Tutorialhelp Comment what about a Thread.sleep inside Share?

 
 

Comment

eric givler
what about a Thread.sleep inside Share?,
January 15, 2008 at 1:06 AM

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?
View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Java Beginners

Thread.sleep - Java Beginners
 

Java-Tutorials

What Is Thread In Java?
 

Java Interview Questions

Thread.sleep timing
 

Java Beginners

thread inside other thread - Java Beginners
 

Java Beginners

Thread
 

Java Beginners

Thread
 

Java-Tutorials

Java : Runnable Thread
 

Java Beginners

Thread
 

Java Server Faces Questions

thread inside other thread - Java Server Faces Questions
 

General

Java Exception Thread
 

JSP Servlet Questions

Confuse about Quartz or Thread. - JSP-Servlet
 

Java-Tutorials

Java :Thread Enumeration
 

Java-Tutorials

Thread Deadlocks - Java Tutorials
 

Java Beginners

java Thread
 

Java Beginners

Inter Thread Communication
 

Java-Tutorials

Java :Thread Methods
 

Java Interview Questions

Java thread
 

Java Interview Questions

Java thread
 

Java Interview Questions

Java thread
 

Java Interview Questions

Java thread
 

Java Interview Questions

Java thread
 

General

Java Daemon Thread
 

Java Beginners

Thread - Java Beginners
 

General

Java Thread In Applet
 

Core Java

Thread Life Cycle Example in java
 

Java Beginners

Thread
 

Hibernate Interview Questions

Java thread
 

Java-Tutorials

Java Thread
 

Java-Tutorials

Create Thread by Extending Thread
 

Java Interview Questions

Java thread
 

Java Interview Questions

Java thread
 

Java Beginners

Thread
 

Java Interview Questions

What values initialized inside init() in servlet - Java Interview Questions
 

Java Interview Questions

Java Thread and Runnable
 

General

Main Thread and Child Thread
 

Java Beginners

Demon thread
 

Java Beginners

Thread
 

Java Interview Questions

Java thread state
 

Java Interview Questions

Thread scheduling
 

Java Beginners

How to Differenciate Main Thread and Child Thread in Java
 

Java Interview Questions

Thread in Nutshell
 

Java Interview Questions

Java thread lock
 

Java Interview Questions

Java thread state
 

General

Java Sleep Thread
 

General

Java Thread
 

Java Beginners

thread
 

Java Beginners

Daemon thread - Java Beginners
 

Java Beginners

Java Thread - Java Beginners
 

Java-Tutorials

Overview of Thread
 

Java-Tutorials

Java :Thread dumpStack
 

Java-Tutorials

Thread Access through the Hash Table in Java
 

Java Beginners

Extending thread - Java Beginners
 

Programming Tutorials

Thread in java
 

General

Java Thread class
 

Java-Tutorials

Java Thread Interrupted
 

Java Beginners

thread class - Java Beginners
 

Java Beginners

java thread - Java Beginners
 

Java Interview Questions

Difference between Timer and Thread?
 

Java-Tutorials

Java Thread destroy
 

Java Beginners

sleep method in thread java program
 

Java Beginners

Thread and Process - Java Beginners
 

Java-Tutorials

Java Thread getId Example
 

General

Java Thread Priority
 

Java-Tutorials

Java Thread : sleep() method
 

Java Beginners

Garbage collector thread - Java Beginners
 

Java-Tutorials

Java Thread : stop() method
 

Java-Tutorials

Java Thread IsInterrupt
 

Java-Tutorials

Thread Creation
 

General

Java Thread setName
 

Java-Tutorials

Thread Synchronization in Java
 

General

Get Thread Name
 

Java-Tutorials

Count Active Thread in JAVA
 

Java Beginners

java thread problem - Java Beginners
 

Java Interview Questions

what is meta data in java
 

Java-Tutorials

Java Thread checkAccess
 

Java Beginners

Thread in java
 

Java Beginners

related to multiple thread....!!!!
 

Java Interview Questions

Java thread
 

Core Java

Threading In Java Example
 

Java Beginners

Java incrementor decrementor thread - Java Beginners
 

Core Java

Thread priority in java
 

Java Interview Questions

Java thread
 

Java Interview Questions

Daemon thread
 

Java Interview Questions

Daemon thread
 

Java Interview Questions

method inside the method??
 

Java Interview Questions

Thread method
 

Java Beginners

what is a thread initial state? how to implement it?
 

Java Beginners

implements runnable n extends thread - Java Beginners
 

Java Beginners

about J2EE. - Java Beginners
 

Java Beginners

Java Function for block inside a block
 

Java Beginners

Java Function for block inside a block
 

Java Beginners

Thread concept
 

Java Interview Questions

Java thread
 

Java-Tutorials

Java Thread setUncaughtExceptionHandlermethod
 

Java Beginners

daemon thread
 

SQL Question

What is the difference between IN and BETWEEN, that are used inside a WHERE clause?
 

Java-Tutorials

Java Thread : setContextClassLoader() method
 

General

Event Dispatcher Thread
 

Java Beginners

How to make one waiting thread for lock - Java Beginners
 

Java Beginners

about J2EE. - Java Beginners
 

Advertisements Advertisements
 

 

 

DMCA.com