Question

I'm working with a GUI and I'm using

Thread.sleep();

in some of the classes, and I'm wondering if I need to create a separate Thread for my Main class (the GUI class), or if each class has a separate Thread by default. Note, the reason that I bring up Thread.sleep(); in the first place is that when working with GUI's Thread.sleep(); pretty much freezes your GUI. Anyways my main question is do I need to create a separate Thread for my Main class or if each class has a separate Thread by default.

Was it helpful?

Solution

Thread.sleep() is a static method of Thread class. Hence, whichever class you place in a method, during runtime, if a thread call flow encounters this class method where Thread.sleep() in invoked, the thread will be blocked for that amount of time.

Your Question:
Anyways my main question is do I need to create a separate Thread for my Main class or if each class has a separate Thread by default.

  • each class has a separate Thread by default -- INCORRECT Statement
    -- Thread class is not inherited by every class
    -- In normal sense, Thread is a call flow. It executes whichever class it encounters thru its method calls.
    -- Class and Thread are 2 separate concepts.
    ---- Class is a definition of an entity,it cannot run by itself, it can be loaded, instantiated with data, method calls can be done and garbage collected.
    ---- Thread is an execution entity at run-time.It can be started, runnable, blocked, stopped. To support this concept, Java has provided Thread class or Runnable interface to extend/implement respectively.

  • do I need to create a separate Thread for my Main class?
    -- your mainclass will be executed in a MainThread which is instantiated and started by your JVM.
    -- It is better to define a seperate user-defined Thread to start your GUI.
    -- If either in the MainThread (or) user-defined Thread, if it encounters Thread.sleep() during call flow, that particular thread will be blocked.

One more thing, your question is not clear on your need for usage of Thread.sleep(). You just have given a reason of the resultant usage of it but not the need for usage.

OTHER TIPS

From javadoc for Thread:

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

Don't mind that the sleep() method is static, it is executed for the thread in which you call it. So if you call it from GUI, it puts your main UI thread into sleep, hence freezing UI completely.

by default the program does not have more than one thread. there should be only one class which has a main method. it does not matter it is a ui application or any other type of application.

please post the details in what scenario you want to use the sleep function.

When a java class is created automatically 3 thread are created by-default with it. 1.Main Thread 2.Thread Scheduler 3.Garbage Collector thread

Please watch this explanatory video to understand what a Thread actually is.

In this example the Thread is the music box, your code is the sheet of paper, and the processing time granted to a thread is the rotation of the handle.

So your main-class code (sheet-of-paper) is inserted into a thread (music-box) generated by the JVM and execution is started (handle rotates). Once you call sleep(1000) "rotations of the handle" is paused for 1 second, and so is the execution of your code. Thread.sleep() is just a short-cut to Thread.getCurrentThread().sleep().

If you now have several threads (music-boxes), you can of course pause one while another still runs. So if you have a main thread and a GUI thread, you can pause the main thread and your GUI will work fine (unless it actually tries to access the main thread). If you however pause the GUI thread, it will appear to be frozen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top