| | |
Multiple threads
![]() |
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 1
Ok, so my problem is this: I did a little java program to take the number of seconds from a text box and sleep a thread for that amount of seconds. Just to make it clear, here is the piece of code I used: Now my problem is how can I make this piece of code shared by a bunch of buttons (without them interfering with one another, like in starting one thread, moving two text boxes to the right, inputing a value then hitting Run on the respective button, and so on). I don't know if this can be done, but it was given to me as an assignment a couple of years ago and I only did it with one thread.
java Syntax (Toggle Plain Text)
jTestare.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { Thread.sleep((Integer.parseInt(getStringInterval()))); } catch (InterruptedException ex) { ex.printStackTrace(); } getLblReprGrafica().setOpaque(true); getLblReprGrafica().setBackground(Color.RED); getLblProcent().setText("100%"); } });
Im not sure I understand your question, but anyway...
First of all, it looks like you're sleeping the main thread, which will cause your application to freeze for a number of seconds.
I might be off here but do you want to fire a new thread for each button but avoid write reduntant code? If so, then create a class that extends Thread and fire it with method start()
fire the thread like:
First of all, it looks like you're sleeping the main thread, which will cause your application to freeze for a number of seconds.
I might be off here but do you want to fire a new thread for each button but avoid write reduntant code? If so, then create a class that extends Thread and fire it with method start()
java Syntax (Toggle Plain Text)
public class MyThread extends Thread { public MyThread(/*argument here if needed*/) { } public void run() { try { Thread.sleep(sleepTime); }catch(InterruptedException ie) { } // Do something here } }
fire the thread like:
java Syntax (Toggle Plain Text)
new MyThread().start();
---------------------------
333 - halfway to hell
333 - halfway to hell
![]() |
Similar Threads
- Is it safe to have multiple threads add to or subtract from a quantity? (Computer Science)
- how to create Multiple threads ??? problem ??? (C++)
- Threads, sockets and TCP (C)
- windows service with threads (C#)
- Help multiple threads accessing buffer structure (Java)
- Threads - need little help (Java)
- HELP for threads (Java)
- Winsock listen on multiple ports (C)
Other Threads in the Java Forum
- Previous Thread: What is the Use of Inner classes
- Next Thread: file transfere on j2me
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





