Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Threads, systems keeps getting frozen

I got a GUI, and when i call the sleep thread method, it keeps freezing the GUI and i cnt use it during that time. and i need to be able to :(

this is the method i am calling :

-------------

 public void runnable(String delay) {
          int count = 0;
          int time = 0;
           time = Integer.parseInt(delay);
          while (count < time) {
              try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) { }
            count++;
        }
               stop();
    }    
-----------------------
0
mkrashid
Asked:
mkrashid
  • 3
  • 2
  • 2
  • +5
1 Solution
 
pronaneCommented:
what is time set as?? the thread may be sleeping to long.
0
 
mkrashidAuthor Commented:
time is set as like e.g 2,5,6 thats how many seconds i want it to sleep for, it sleeps for the right amound of time, as i did a system.out.printlin and it works ok, its just that during this, it freezes up the GUI, and i cnt do nothing else.
0
 
pronaneCommented:
what do you want to sleep?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
girionisCommented:
 What happens if you add Thread.yield(); in the while loop? The GUI functionality is taking place in another thread and it could be waiting for the main thread to finish.
0
 
pronaneCommented:
yes exactly and thats why its freezing cos the thread is sleeping and the gui is waiting for it to finish.
0
 
CEHJCommented:
You shouldn't be sleeping the GUI thread normally. If you want to do whatever that code is doing you posted, use a separate thread.
0
 
wide_awakeCommented:
You could try using the Timer class instead of using Thread.sleep()

0
 
mkrashidAuthor Commented:
i am reading a dat file and the numbers which are read in are converted from string to int and placed in 'time' which determines how long the thread should sleep for, but i want the rest of the gui to bbe active, i am new to threads and if possible could sumone show code example please  
0
 
wide_awakeCommented:
Either a thread is asleep, or it is active.  It can't be both.

If you want something to happen after X seconds, then an instance of Timer will do just that.

from the api docs:

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener()
  {
      public void actionPerformed(ActionEvent evt)
      {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

0
 
objectsCommented:
All GUI processing is handled in a single thread. If you block that thread (using sleep() for instance) then your GUI will freeze.
You need to do your processing in a seperate thread as suggested by CEHJ.
0
 
CleanupPingCommented:
mkrashid:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now