Solved

java programming in Thread

Posted on 2004-10-03
4
203 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
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 78
why cannot we forward request once the response is committed 2 42
ForLoop Example 3 49
MySQL  on Tomcat 8 46
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

778 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