Query on  Thread.setDaemon method......

I have a small conceptual problem.
I wrote a small program which starts a thread
from it's main method and marks it as Daemon.
The Thread actually starts an infinite loop.
Inside the run method of another Thread I start another Thread
which is also an infinite loop. I mark it as non-daemon(although I don't need to as default is daemon).
But when I ran the program I found that the program terminates
after some time. As What I know about threads it should not have stopped
becz. a non daemon thread was running. Plz. explain it.
Both the Classes are paster below.
Nimesh
/*Class::Test 1*/
public class Test1 implements Runnable{
private static Test2 test2=null;
private static Thread thread2=null;
public static void main(String args[]){
Test1 test2=new Test1();
Thread thread1=new Thread(test2);
thread1.setDaemon(true);
thread1.start();
public void run(){
test2=new Test2();
thread2=new Thread(test2);
thread2.setPriority(Thread.NORM_PRIORITY );
thread2.setDaemon(false);
thread2.start();
System.out.println("Got here");
while(true){
func1();
public void func1() {
System.out.println("In Thread 1");
/*Class: Test1*/
public class Test2 implements Runnable{
public void run(){
while(true){
func1();
public void func1(){
System.out.println("In Thread2");

I think that you have two problems
1) there is a chance that the second thread would not have started, because i think the start method does not actually garuantee that the second thread is actually started when the method returns. If you want to garuantee that the second thread has started you need to wait on some synchronized block of code in thread one after you have called the start method for thread two. Then thread two needs to call a notify method in its run method.
2) Your main method is exiting before all the threads are starting, i would put a sleep of say 1ms to let the other threads start or a syncrhonized block.

Similar Messages

  • Query on Thread

    Hi ,
    I have a query on Thread in Java.
    In the class Thread there is a method named start(), which when called , invokes another method run().Then why we do not call the method run() directly ,instead of calling start() ?
    Regards.
    Ayan

    run() runs in the thread it was called from. So if you want another thread, you need to call start(). but if you want to run another thread's run() in your thread, by all means call run()

  • How to throw Exception in Thread.run() method

    I want to throw exception in Thread.run() method. How can I do that ?
    If I try to compile the Code given below, it does not allow me to compile :
    public class ThreadTest {
         public static void main(String[] args) {
         ThreadTest.DyingThread t = new DyingThread();
         t.start();
         static class DyingThread extends Thread {
         public void run() {
         try {
                   //some code that may throw some exception here
              } catch (Exception e) {
              throw e;//Want to throw(pass) exception to caller
    }

    (a) in JDK 1.4+, wrap your exception in RuntimeException:
    catch (Exception e)
    throw new RuntimeException(e);
    [this exception will be caught by ThreadGroup.uncaughtException() of this thread's parent thread group]
    In earlier JDKs, use your own wrapping unchecked exception class.
    (b) if you know what you are doing, you can make any Java method throw any exception using Thread.stop(Throwable) regardless of what it declares in its "throws" declaration.

  • Confusion using Thread.sleep() method

    I tried it in this program
    class NewThread implements Runnable {
      String name; // name of thread
      Thread t;
      NewThread(String threadname) {
        name = threadname;
        t = new Thread(this, name);
        System.out.println("New thread: " + t);
        t.start(); // Start the thread
      // This is the entry point for thread.
      public void run() {
        try {
          for(int i = 5; i > 0; i--) {
            System.out.println(name + ": " + i);
            Thread.sleep(1000);
        } catch (InterruptedException e) {
          System.out.println(name + "Interrupted");
        System.out.println(name + " exiting.");
    class MultiThreadDemo {
      public static void main(String args[]) {
        new NewThread("One"); // start threads
        new NewThread("Two");
        new NewThread("Three");
        try {
          // wait for other threads to end
          Thread.sleep(10000);
        } catch (InterruptedException e) {
          System.out.println("Main thread Interrupted");
        System.out.println("Main thread exiting.");
    }the output is
    one: 5
    two: 5
    three 5
    delay of 1 second
    one:4
    two: 4
    three: 4
    delay of 1second
    what all i know about sleep() is that it pause the currently executing thread for specified time. hence in above program when first thread runs and prints value 5 then it should get paused for 1 second cause there is Thread.sleep(1000) after println() statement.
    and output should be
    one: 5
    delay of 1 second
    two: 5
    delay of 1 second
    three: 5
    delay of 1 second
    but this is not actually happens. may be i am wrong about Thread.sleep method() or something else.
    would you guys please clear it?
    regards
    san

    Thread.sleep() only sleeps the current thread. As each thread runs concurrently they all print concurrently and all sleep concurrently.

  • Thread start() method problem

    I have a program server-client program that have a lot thread but
    when I called thread.start() method it doesn't return run() method.
    even so my input-output codes is running ( my client program can send data to server program )
    Please help me

    yasinmalli wrote:
    start() method doesn't call run() method of thread when I debug row to rowIt does, but the debugger will only ever follow the flow of the current thread and therefore not step into the run method. Set a breakpoint at the very beginning of your run() method and then step over the start() method, you'll see that the new thread will break exactly at that breakpoint just after start() was executed.

  • How to stop a thread without the deprecated Thread.stop() method?

    Hi,
    I am writting a server application that launches threads, but the run() implementation of these threads are not written by me (i.e. i have no control over them): they are third-party programs. That's why i could not use the well known Java tutorial way to stop a thread (i.e. with a global variable that indicates the thread state).
    I would like my server to be able to stop these threads at any time, but without using the deprecated Thread.stop() method.
    Any ideas ?
    Thanks in advance,
    Fabien

    Thanks Pandava!
    I was arrived at the same conclusion... As to me, it is a very bad issue, because it means for example that a servlet server can not stop any servlet it launches (especially for preventing infinite loops).
    If i want to be strictly JDK 1.4 compliant, i should not use Thread.stop(). But if i don't use it, i don't have any ideas of how stop a thread that i don't control...

  • Thread.run() method..

    I was wondering how the "Thread.run()" method get called. I started with "Thread.start()" and found it calls the "Thread.start0()" which is a native method.
    I downloaded the JDK 5 source code and went to "j2se\src\share\native\java\lang\Thread.c". In "Thread.c" I found a mapping of "start0()" with "JVM_StartThread".
    After that I opened the "hotspot\src\share\vm\prims\jvm.cpp". From line no 2257 to 2316 I saw the implementation of "JVM_StartThread".
    In side "JVM_StartThread", I saw there is a call to "Thread::start(native_thread)" and opened the "hotspot\src\share\vm\runtime\Thread.cpp". In "Thread.start()" method there is a call to "os::start_thread(thread);".
    In "hotspot\src\share\vm\runtime\os.cpp" the "os::start_thread()" calls the "pd_start_thread(thread);" method. But I did not get the "pd_start_thread(thread);" method in os.cpp.
    Note: I am java programmer and hardly understand the C/C++ languages. But had a doubt how the run() method get called and how the Thread.start() method works. But I got stuck here.
    Kindly help me to know how the flow goes, when we call the Thread.start() and how the run() method is linked to it.

    But I did not see anything in the os.cpp. In fact i did not find the "pd_start_thread(thread);" .
    void os::start_thread(Thread* thread) {
      // guard suspend/resume
      MutexLockerEx ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
      assert(thread->is_baby_thread(), "thread has started");
      thread->clear_is_baby_thread();
      OSThread* osthread = thread->osthread();
      // A thread may be suspended in the presence of the profiler.
      // Only start thread when it's not suspended.
      osthread->set_state(RUNNABLE);
      if (!thread->is_vm_suspended()) {
        pd_start_thread(thread);
    }

  • Parallel query worker thread was involved in a deadlock

    Hi,
        We are getting deadlock issue in MSSQL 2012 enterprise edition. By enabling the trace we could find the MORE number messages as below: 
    "parallel query worker thread was involved in a deadlock"
    Along with this there are some other entries with dead lock information ( chain, graph) . 
    Please suggest.
    Thank you 

    OP, I think that article posted by Saeid is exactly about your situation, although SQL2012 might have changed the message slightly.
    What the article says is bad news - you have done nothing wrong, yet SQL Server is messed up.  The only "fix" recommended is eliminating parallelism by using a hint "option (maxdop 1)" at the end of your query.  This should do the trick, but may
    run more slowly.
    You can try to reduce the odds of it happening by adding indexes.  Actually, ANY change to your plan or data model may make just enough changes to come up with another plan that does NOT get these self-deadlocks.  Any query that is more efficient,
    should reduce the odds of a deadlock.
    You can also try to analyze the deadlock as you would any deadlock, it may suggest another way to write the query. 
    Josh

  • Difference between LockSupport.parkUntil and Thread.sleep method

    Hi
    Could someone please explain the difference between LockSupport.parkUntil and Thread.sleep methods.
    Thanks in advance.

    javaLearner wrote:
    The thing that I am trying to understand is - the difference / correlation between the two methods mentioned.
    In the javadoc, there is a mention of correlation between the LockSupport methods park, unpark and the Thread methods suspend, resume - but not park and sleep.Then that suggests that there isn't any difference /correlation -- whatever you meant by that. Perhaps your question is like asking for the difference between a fish and a bicycle.
    Also, I would like to understand the statement
    These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications. (from the javadoc)Again, you're going to have to explain what you don't understand about it.

  • Setting optional parameter stepToStoreResultsIn of Thread.WaitForEnd() method

    This question is  related to an earlier posting of mine regarding launching a thread from a c++ code module (http://forums.ni.com/ni/board/message?board.id=330&message.id=15383&query.id=775150#M15383). I am launching a new thread from a code module (c++ DLL), and I need wait for it to finish.
    There are 2 ways I envision doing this:
    1. wait for the new thread to finish before returning from my code module by calling Thread.WaitforEnd()
    2. return a reference to the new thread to the calling sequence and make the calling sequence wait for the thread to complete before terminating using a wait step at the end of the sequence. 
    My questions are regarding method 1, where I would call Thread.WaitforEnd(). I want to set the parameter stepToStoreResultsIn to set the result of the step in the calling sequence to the result of the sequence executed in the new thread:
    a. Do I understand the purpose of the stepToStoreResultsIn parameter correctly?
    b. The stepToStoreResultsIn parameter must be a variant, so how do I pass in the step context as a variant? is it the same as a IDispatch pointer?

    a. Yes
    b. Yes

  • How to get value from Thread Run Method

    I want to access a variable from the run method of a Thread externally in a class or in a method. Even though I make the variable as public /public static, I could get the value till the end of the run method only. After that it seems that scope of the variable gets lost resulting to null value in the called method/class..
    How can I get the variable with the value?
    This is sample code:
    public class SampleSynchronisation
    public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run method "+sathr.x);
    /* I should get Inside the run method::: But I get only Inside */
    class sampleThread extends Thread
         public String x="Inside";
         public void run()
              x+="the run method";

    I think this is what you're looking for. I hold up main(), waiting for the results to be concatenated to the String.
    public class sampsynch
        class SampleThread extends Thread
         String x = "Inside";
         public void run() {
             x+="the run method";
             synchronized(this) {
              notify();
        public static void main(String[] args) throws InterruptedException {
         SampleThread t = new sampsynch().new SampleThread();
         t.start();
         synchronized(t) {
             t.wait();
         System.out.println(t.x);
    }

  • Error while executing query on portal-SAPMSSY1 method : UNCAUGHT_EXCEPTION

    Hello all,
    I have created a quey and executed it on portal and everything just works fine. The query has few input varaibles at the start like fiscal year and some other inputs. I saved the query in my Portal favorites and then when I try to excute it from there it gives me a runtime BI error.
    Termination message sent ABEND RSBOLAP (000): Program error in class SAPMSSY1 method : UNCAUGHT_EXCEPTION  MSGV1: SAPMSSY1  MSGV3: UNCAUGHT_EXCEPTION
    I guess it errors out because of the variable input part at the start of query.
    We already have note "950992" in the system so wonder what next now.
    Any inputs appreciated.
    Thanks,
    Kiran

    We are having the same issue too - we are SP13. Our query is built on a Infoset, and the problem comes with 0FISCPER variable, when we try to do a input help (F4) in Bex. this happens only when I use fiscper on queries bulit on INFOSETS, and it works perfectly alright with all other cubes.
    <i>System error in program CL_RSMD_RS_SPECIAL and form
    IF_RSMD_RS~READ_META_DATA-02 (see long text)
        Message no. BRAIN299
    Diagnosis
        This internal error is an intended termination resulting from a program
        state that is not permitted.
    Procedure
        Analyze the situation and inform SAP.
        If the termination occurred when you executed a query or Web template,
        or during interaction in the planning modeler, and if you can reproduce
        this termination, record a trace (transaction RSTT).</i>
        For more information about recording a trace, see the documentation for
        the trace tool environment as well as SAP Note 899572.
    and in portal, it gets to the point of variable screen and then throws a error msg.
    <i>ABEND RSBOLAP (000): Program error in class SAPMSSY1 method : UNCAUGHT_EXCEPTION
      MSGV1: SAPMSSY1
      MSGV3: UNCAUGHT_EXCEPTION</i>
    Message was edited by:
            voodi
    Message was edited by:
            voodi

  • SQL query issue in a method

    I am trying to use a method to query a database to gather a person's name when the Social Security Number is entered. However in the display, it seems to only bring back the field that you use to compare in the load statement. Can anyone tell me what I am doing wrong? The below example will bring back the SSN but if I change the last line in the example to HISTORY.fullName I get a "Null Value" returned back. Thanks.
    input "Social Security Number of Employee to terminate: " : intSSN
    //intSSN is a local variable
    using title = "Termination Applet",
    buttons = ["OK", "Cancel"]
    returning selectedButton = selection
    //pg 310 of Fuego 5.5 document
    //Database name is HISTORY, local variable is ssn, seems to only only display what is in load
    //and nothing else
    load BpmDB.Dbo.HISTORY using ssn = intSSN
    display HISTORY.ssn

    load BpmDB.Dbo.HISTORY using ssn = intSSN
    display HISTORY.ssnTry doing:
    load BpmDB.Dbo.HISTORY using ssn = intSSN
    display HISTORY //Note the difference, we're asking for the whole object,
    not just the ssn
    Juan
    On Thu, 07 Sep 2006 13:52:39 -0300, Ben Sfanos wrote:
    I am trying to use a method to query a database to gather a person's
    name when the Social Security Number is entered. However in the
    display, it seems to only bring back the field that you use to compare
    in the load statement. Can anyone tell me what I am doing wrong? The
    below example will bring back the SSN but if I change the last line in
    the example to HISTORY.fullName I get a "Null Value" returned back.
    Thanks.
    input "Social Security Number of Employee to terminate: " : intSSN
    //intSSN is a local variable
    using title = "Termination Applet",
    buttons = ["OK", "Cancel"]
    returning selectedButton = selection
    //pg 310 of Fuego 5.5 document
    //Database name is HISTORY, local variable is ssn, seems to only only
    display what is in load
    //and nothing else
    load BpmDB.Dbo.HISTORY using ssn = intSSN
    display HISTORY.ssn

  • How can I get the variable with the value from Thread Run method?

    We want to access a variable from the run method of a Thread externally in a class or in a method. Even though I make the variable as public /public static, I could get the value till the end of the run method only. After that scope of the variable gets lost resulting to null value in the called method/class..
    How can I get the variable with the value?
    This is sample code:
    public class SampleSynchronisation
         public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run method "+sathr.x);
    // I should get Inside the run method::: But I get only Inside
    class sampleThread extends Thread
         public String x="Inside";
         public void run()
              x+="the run method";
    NB: if i write the variable in to a file I am able to read it from external method. This I dont want to do

    We want to access a variable from the run method of a
    Thread externally in a class or in a method. I presume you mean a member variable of the thread class and not a local variable inside the run() method.
    Even
    though I make the variable as public /public static, I
    could get the value till the end of the run method
    only. After that scope of the variable gets lost
    resulting to null value in the called method/class..
    I find it easier to implement the Runnable interface rather than extending a thread. This allows your class to extend another class (ie if you extend thread you can't extend something else, but if you implement Runnable you have the ability to inherit from something). Here's how I would write it:
    public class SampleSynchronisation
      public static void main(String[] args)
        SampleSynchronisation app = new SampleSynchronisation();
      public SampleSynchronisation()
        MyRunnable runner = new MyRunnable();
        new Thread(runner).start();
        // yield this thread so other thread gets a chance to start
        Thread.yield();
        System.out.println("runner's X = " + runner.getX());
      class MyRunnable implements Runnable
        String X = null;
        // this method called from the controlling thread
        public synchronized String getX()
          return X;
        public void run()
          System.out.println("Inside MyRunnable");
          X = "MyRunnable's data";
      } // end class MyRunnable
    } // end class SampleSynchronisation>
    public class SampleSynchronisation
    public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run
    method "+sathr.x);
    // I should get Inside the run method::: But I get
    only Inside
    class sampleThread extends Thread
    public String x="Inside";
    public void run()
    x+="the run method";
    NB: if i write the variable in to a file I am able to
    read it from external method. This I dont want to do

  • How can I get the query name in webitem method ?

    Hello,
    I have defined a new web item but I can't retrieved parameter of my webitem in the RENDER method for instance. I have declared a parameter in the RSRRENDERATR table like this :
    REN_NAME : MY_WEBITEM
    ATR_NAME : MY_PARAM
    ATR TYP : TEXT
    CHR MAX LEN : 50
    How can I do this ? How can I get the MY_PARAM value in the RENDER method ?
    Thanks a lot
    GC.
    Edited by: CoGr on Feb 11, 2008 12:10 PM

    Hi ,
    data l_r_view type ref cl_rsr_www_view.
    data l_t_text_symbols type rrx1_t_txt_symbols.
    l_r_view =? n_r_data_provider.
    CALL METHOD L_R_VIEW->n_r_request->TEXT_ELEMENTS_GET
      IMPORTING
        E_T_TXT_ELEMENTS = l_t_text_symbols.
    l_t_text_symbols contains all DP information like Query technical name, or Report text last load etc.
    best regards,
    kai

Maybe you are looking for

  • I've had this 2D array problem for ages and would apprecoate any help :)

    I've posted a few times about this in the past and with your guys help I have gradually advanced. Basically, all I want to do at the moment is fill the lowest position a counter can go in in a game of Connect Four. The first counter goes to the botto

  • Multiple Collapsible Panels on same page

    I am creating a new site using Dreamweaver CS3, temporarily uploaded at: http://www.sharpeacademy.co.uk/nick-jackson.co.uk/sharpeacademy/theatrearts/ I have an email signup form using a collapsible panel (this appears on every page). I want to put a

  • Photoshop CS5 question - trying to save a name with extension .jpg or .png

    Hi, If I open a new file and go to save it the extension names show up (.jpg or .png). However, if I open an illustrator file in Photoshop and try to save it the extension names are not there (it's just saving them as the proper format, but I want th

  • I am so mad at apple!

    my track pad button broke and so i took it to comuspa. they sent it in to apple and when i got it back they replaced the keyboard and the button. well the button now sticks sometimes and i noticed my computer running slower and lagging more than it u

  • IOS 7 mail app security concerns

    I just witnessed something that made me very concerned about the iOS 7 Mail App security. I was changing my mail account password. (I am still stuck with a Hotmail account.) So after I changed my Hotmail account password in a desktop web browser, I w