943,807 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 414
  • Java RSS
Jul 1st, 2009
0

Multiple threads

Expand Post »
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:
java Syntax (Toggle Plain Text)
  1. jTestare.addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent evt) {
  3. try {
  4. Thread.sleep((Integer.parseInt(getStringInterval())));
  5. } catch (InterruptedException ex) {
  6. ex.printStackTrace();
  7. }
  8. getLblReprGrafica().setOpaque(true);
  9. getLblReprGrafica().setBackground(Color.RED);
  10. getLblProcent().setText("100%");
  11. }
  12. });
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
adyopo is offline Offline
9 posts
since Feb 2008
Jul 1st, 2009
0

Re: Multiple threads

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()

java Syntax (Toggle Plain Text)
  1. public class MyThread extends Thread {
  2. public MyThread(/*argument here if needed*/) {
  3. }
  4.  
  5. public void run() {
  6. try {
  7. Thread.sleep(sleepTime);
  8. }catch(InterruptedException ie) {
  9. }
  10. // Do something here
  11. }
  12. }

fire the thread like:
java Syntax (Toggle Plain Text)
  1. new MyThread().start();
Reputation Points: 22
Solved Threads: 13
Junior Poster in Training
di2daer is offline Offline
66 posts
since Sep 2008
Jul 2nd, 2009
0

Re: Multiple threads

Thanks di2daer, I'm gonna try to modify my code accordingly and see what happens.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
adyopo is offline Offline
9 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: What is the Use of Inner classes
Next Thread in Java Forum Timeline: file transfere on j2me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC