Solved

java programming in Thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hibernate Not Rolling Back When something fails in a stored procedure 2 52
@SBGen Method 3 38
eclipse argument 14 53
configure dependency in POM for new database 3 18
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

19 Experts available now in Live!

Get 1:1 Help Now