Solved

java programming in Thread

Posted on 2004-10-03
4
207 Views
Last Modified: 2010-03-31
public class mythread extends Thread
{
      private boolean continuerun;
      public void setcontinuerun(boolean setc)
   {
         continuerun=setc;
   }
      public void run()
      {
            continuerun=true;
            while(continuerun)
            {
                  System.out.println("Iam running");
         }
                  System.out.println("Iam tired of running");
}
      public static void main(String args[])
      {
            mythread mm=new mythread();
            mm.start();
            //try
            //{
            //      Thread.currentThread().sleep(1000);
            //}
            //catch(InterruptedException ie)
            //{
      //}
            mm.setcontinuerun(false);
}
      }
My question is when is only when i add the sleep() method i could see the statement "Iam tired of running" ,why?
0
Comment
Question by:sraveend
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Assisted Solution

by:TrekkyLeaper
TrekkyLeaper earned 250 total points
ID: 12213151
The call to setcontinuerun(false) was coming too soon. I am guessing, you were seeing an infinite loop of "Iam running". When you call start(), it does not switch context to that thread immediately. When you called sleep(), the context had a chance to change to the "mm" thread.

To restate, without the sleep, you were setting continuerun to false before you had even set it to true and gotten into the loop. With the sleep, you get into the loop before you change the boolean variable.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213172
Possibly starvation of the explicitly created thread. Try

mm.setPriority(10);
0
 

Author Comment

by:sraveend
ID: 12213216
Could u suggest me any usefull link related to thread and synchronization
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 12213227
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question