Hey folks, have something that I suspect is an easy problem to resolve but I don't know enough yet about how threads work so ... My main class I have using the Runnable interface and creating a new thread it. It calls a method in another helper class which does alot of calculations and as it goes it updates the text ... |
I'm working on my mini project for my first java exam. It is a Ludo-type game. I already made an animation for this (rolling the dice, moving the objects) but I introduced a mouseListener for the "Roll the dice" button and the animation isn't working, I see only the last picture. I realised that it's caused by Thread.sleep(), that blocks the ... |
First some background on my project: I'm developing a simple game in order to learn Java, and it is two player. A Player is a child of Thread, and the thread continually reads from the player's input buffer (an array list) and processes movements. A ComputerPlayer is a child of Player and it makes random moves and also reads from it's ... |
No. Let's assume for a moment the code you posted above appears in the main() method of your application. This main() method is executed on the main thread. What happens in your code is that a new thread t is created then started, all on the main thread. After t.start() has been called, there are two threads running "at the same ... |
|
i have a multithreaded client which i would like to be able to sleep indefinitly while waiting for input from a server at random intervals. how can i use thread.sleep() or some other method to sleep indefinitly until interrupted by new data in an input stream.? oh and after servicing the input id like it to go back to sleep or ... |
|
|
Hi!. I have the following code: public void X(){ System.out.println("Hi"); Thread.sleep(3000); System.out.println("Bye"); } When I compile this program, the following error ocurr: Lamp.java:46: unreported exception java.lang.InterruptedException; must be ca ught or declared to be thrown Thread.sleep(3000); ^ 1 error Someone please can help me with the correct syntax of thread.sleep?. I was looking for it in google, but I dont found ... |
Hi, I have a program that needs to wait for a period of time before it continues. I use Thread.sleep(5000) to accomplish this. However, before it sleeps it is meant to change the text of a label using labelname.setText("Hello World"); For some reason it sleeps before changing the label. Is there a way to fix this? The sleep command is sent ... |
You don't want to be doing Thread.sleep or any of your "business" logic in the Even Dispatch Thread, which is I assume what calls that paint method. I don't do GUIs, but I think you'll want to move your loop and coordinate adjustments and sleep call out to a separate method, and use invokeLater to call it. |
I have a program that has a swinging bar, and the more levels you advance, the faster the bar goes. For example, level one, speed = 10; level two, speed = 9; ect. And then in my run() I use Thread.sleep(speed); The problem is, the transition from like 2 to 1 is phenomenal, while 2 seems pretty slow 1 seems unreasonably ... |
Hi I am creating thread which calls a .bat file to start one other java program. I start this thread from main thread. Now I am halting main thread for two min by calling Thread.sleep(120 * 1000). That means main thread should stop for 2 min but thread which i started from main thread must run but that thread also go ... |
'cause I've called the sleep method thrice on main thread, so thread1 has a chance to complete first, but main thread is the first to finish despite of calling the sleep method on it. and I've also called interrupt method explicitly, but have not got any interrupted execption. why is that so? Thanks in advance. |
|
Hey Darryl, Can you tell me if I need a separate JPanel in an applet to draw? If I choose to put a JPanel in an applet, how do I specify the drawing goes in there instead of the JApplet? I'm heading to check out the custom painting link now. Thanks, Jeff |
|
Actually there are more than 14000 members in our website and I have to send them newsletter. I am getting news from oracle db and sending via JavaMail. The problem is that after sending 800/900 mails it gives timeout error. then I split the members into different categories and each category not have more than 500 email. Then my code start ... |
I'm sorry I don't understand that at all or know where to put it to get it to work. Isn't there a simple way of getting the loop to pause and then my selection to redraw? It was two lines of code in C++, is this something to do with the Java Virtual machine not knowing the time? I'm a noob, ... |
Hi, It seems that Thread.sleep is inaccurate : Date d1 = new Date(); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Date d2 = new Date(); System.out.println(d2.getTime() - d1.getTime()); I ran this multiple times and it returns me either 187 or 188 either 203 or 204 Is there any explanation about this ? Thank ... |
If you want to catch all possible exceptions, do a catch(Throwable), not catch(Exception). Other than that I cannot say why this behavior is occurring from the code you posted. You do always get the log line before the sleep call (in other words, is the thread even put to sleep)? Perhaps you will want to switch to a Timer structure in ... |
|
We are using Quartz, but this is a different issue. I need to know how the JVM determines when a thread should be woken up based on it's sleep time. Is it "x time is reached so wake up" type or is it "wake up when the thread has slept for x millis". If it's the former, then sleep time + ... |
Thread.sleep() sleeps the current thread, just because there is a call on a method on another object does not make a different thread sleep. You cannot make a different thread sleep this way. You can use synchronized blocks but its not a very useful construct. IMHO. I assume this is just an exercise. Its worth noting that threads don't start immediately ... |
I have a GUI with some buttons. One button turns this loop on and the otherone is supposed to stop it. How do I go around doing that? Shouldn't I put some condition in the while loop and make the "stop" button change that condition? When I push the "on" button the GUI halts so I can't control anything anymore. |
"implementation-dependant" usually means, you should need to know and you shouldn't rely on the behaviour of one version of a JVM. However, AFAIK most threads are native threads supported by the OS and sleep is supported by the OS too. The OS has a scheduler and it decides when a thread is started or switched. |
Hey Guys, I had the sleep line Thread.sleep(0, 5); running under JVM 1.6.0.07 and it seemed to have worked (now i understand it did not do what i expected) and under JVM 1.6.0.12 i found that it does not sleep at all, as it seems. I used the sleep method to lower CPU usage - used sleep on short loops. Does ... |
I have also tried setting a boolean in a synchronized block than can tell the thread to wait(), and so instead of sleeping, setting the thread to wait and scheduling a TimerTask to wake the thread back up. I have the same result. But I may not be doing this right, so if it sounds like this is a good idea, ... |
|
|
Now my question is , I only have single threaded application and am instantiating same application 'app1' for different parameters/arguments. I know this will work but wanted to know if there are any gotchya's to this approach or better programming than this( Since allocating memory for each instance). Maybe, and yes Looking at Sun's thread document , Multi threaded application may ... |
|
Hello, I have an assignment in which I must process jobs in a job queue. Each job must run for a specific amount of time (4 seconds, 8 seconds, etc). I have 15 jobs (Job objects I created) and 10 memory blocks (an array of Memory objects). So, for example, I would like a job to go into memory block 1 ... |
I have an application that I want to guarantee that when I call Java's Thread.sleep(n) the thread will sleep for precisely that amount of time, or close to it at least. What we recently encountered though was that if the "wall clock" time is changed in one thread while a sleep is occurring in another thread, this clock change will impact ... |
Could you please elaborate on that? Can the JVM interrupt any thread anytime and cause to wake it? If a thread is sleeping, can in any case, the JVM on its own interrupt it and wake it up although its scheduled to wake for some more time? Please point me to the correct location for this information. Thanks. |
class RowlevelLocking extends Thread implements Runnable { Thread x,y; RowlevelLocking() { x = new Thread(this,"a"); y = new Thread(this,"b"); x.start(); y.start(); } public static void main(String arg[]) { new RowlevelLocking(); } public void run() { while(true) { display(); } } private void display() { System.out.println("welcome......."); x.sleep(1000); } } here if i sleep the x thread the y thread also sleep............... what ... |
One more thing. How do you make the whole thing sleep for a while without suddenly making the frame transparent? I don't have anything specific to do every X seconds, just to make it pause for X seconds. Any way to do that? And how come Thread.sleep() works just fine if there's no ActionListener? Edited by: JavaGamer on Jun 19, 2008 ... |
Hi, I am using the method Thread.sleep(millis, nanos) and i want my thread to sleep for a very short time. I am using it in a way that will make the thread sleep for 10 millis - Thread.sleep(0, 10) - is it really making my thread sleep for that short a time or is there a minimum used by Java for ... |
I'm programming a memory and I tried to use Thread.sleep when the second card is selected the program should "sleep" for 1 sec before flipping the cards back. I've also thought about using Timer instead but I thought Thread.sleep would be simpler because it automaticly prevents any more events. Sorry if I'm asking stupid questions but excactly why shouldn't I use ... |
|
Hi, Sleep - holds the lock, while it sleeps and releases the lock after the sleeping state. In the program below, I tried with sleep(300) and its not holding the lock while it sleeps. This actually works like wait(300), where it doesn't hold the lock in the waiting state. Can anyone please explain me this. Is there any specific scenarios, I ... |
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. What's wrong with this description? Okey, may be sample will be more easy to understand. Imagine you are monitoring state of webserver and need to ... |
|
The rule is: never do anything that takes more than a fraction of a second on the dispatcher thread. Your code is running on the dispatcher thread if it's invoked by any of the GUI callbacks and listeners, such as ActionListener. Until a listener returns the whole GUI is frozen because it only does one thing at a time, and executing ... |
|
Hi Guys, Is there a different way of letting my class wait for 10 seconds instead of using Thread.sleep(). I can't use Thread.speep() as it keeps getting Interrupted and comming out off sleep. It is a multithreaded application, with threads listining on other threads, so I can't use sleep() method Thankyou Sunath Kodali |
There are two Timer classes in the API, one in java.util and one in javax.swing. These objects can easilly be set up to invoke your code every second. Of course they use threads, but you don't have to worry about them. Incidentally you don't make classes implement thread, you make them implement Runnable and create a Thread object pointing to them. ... |
need help... where should i add a prompt for the user to say that there's no action done, if during the thread.sleep() the disk drive is not inserted? bytheway how does the Thread.sleep(500); works? the code are as follows: public static void main(String[] args) { File f = new File("f: "); System.out.println("Insert disk drive..."); while (!f.exists()) try { Thread.sleep(500); } catch ... |
Let me just update my post. This is what I want. 1) Enter some text into JTextField 2) Press 'Enter' from JTextField 3) Show in JLabel some text 3) Stop (pause) for 1 seconds (Thread.sleep(1000)) 4) Continue program Now, JTextField has KeyListener with keyPressed method for ENTER key and when ENTER is pressed from keyPressed another method in another class is ... |
|
|
|
I can suggest you a possible alternative. Not sure if its works. But is specific to the OS u r using.Use Java's runtime to execute a sleep on UNIX, which will pause the process for the desired amount of time. something like Runtime.getRuntime.exec("sleep 20"); Well, Runtime.getRuntime().exec("explorer"); opens up an explorer for u on windows. Then the sleep on unix shud also ... |
|
OS: Win7 32bit CPU: i3 370M (They said its 2 cores and 4 Threads) When I run it in this environment,the redResult sometimes contains duplicate numbers. But I thought I had remove the number from redBall list after got it. Is this a JDK bug? If not,what shoud I do to achieve my purpose? Edited by: EJP on 15/02/2011 09:39 - ... |
At least half the time I use it, I'd want it to be, the other half I wouldn't care. What are you doing with it where this might be a problem? Changing the system clock abruptly can cause all kinds of problems with your system anyway, because various background activities etc. often depend on file dates. Wherever possible use the "skew" ... |