creating multiple threads ?

creating multiple threads ?

I have implemented a Thread in my java program, by implementing Runnable interface.

& i wrote the desired content in the run().

Like this;

class Test1extends JFrameimplements Runnable,ActionListener{

public Test1(){

.....

Thread t1 =new Thread(this);

t1.start();

.....

...

}

publicvoid run(){

while(true){

..........

.......//do the tasks

....

}

}

publicvoid actionPerformed(ActionEvent e){

Object source = e.getSource();

if(source==btn1){

//task1

//subtask1.1

//subtask1.2

}elseif(source==btn2){

//task2

//subtask2.1

//subtask2.2

}

}

publicstaticvoid main(String args[]){

Test1 tst =new Test1();

}

}

Problem is how can I assign actionPerformed method to another thread.

(So that, subtask1.1 & 1.2 running on different threads)

Things in actionPerformed method should run in another thread

Can i have another new Thread (say t2) below the thread t1. (t1 above created)

Thread t2 =new Thread();

if so, what should be put inside the().

(Earlier i putthis

. )

pls help

[2646 byte] By [jugpa] at [2007-10-3 4:18:15]
# 1
How about just putting the stuff you want to do into a Runnable, and run that from actionPerformed?> class Test1 extends JFrame implements Runnableis just sick.
CeciNEstPasUnProgrammeura at 2007-7-14 22:19:54 > top of Java-index,Java Essentials,Java Programming...
# 2

JFrame implements ActionListener is pretty obsolete too. You need to learn to use (probably anonymous) inner classes. I usually use Action objects.

I'd do something like:

Action actionOne = new AbstractAction("Function 1") {

public void actionPermormed(ActionEvent e) {

setEnabled(false); // block second click

Thread t = new Thread(new Runnable() {

public void run() {

... first action code hear

setEnabled(true); // we're done - reenable control

});

t.start();

}

}

}

Then you just use actionOne as the parameter when you create your JButton, JMenuItem etc.

malcolmmca at 2007-7-14 22:19:54 > top of Java-index,Java Essentials,Java Programming...
# 3

It's technically incorrect to update Swing components in any thread other than the Event Dispatch Thread, and doing so can sometimes result in the component appearing to be not updated. So setEnabled and other component update methods should be called in the finished() method of a SwingWorker thread. (or some other way to make sure the method call happens in the Swing EDT)

BinaryDigita at 2007-7-14 22:19:54 > top of Java-index,Java Essentials,Java Programming...
# 4
Its not a good idea to start a thread within your constructor. You can introduce a data race into your class is to expose the this reference to another thread before the constructor has completed. http://www-128.ibm.com/developerworks/java/library/j-jtp0618.html
kilyasa at 2007-7-14 22:19:54 > top of Java-index,Java Essentials,Java Programming...