Solved

java programming in Thread

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 86
endX challenge 2 50
base64 decode encode 12 96
maven example error 3 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now