How to create new thead

Hi,

I have main class that is Swing form. In this class I want to invoke second class which implements Runnable interface:

e.g.

publicclass mainClass

{

publicstaticvoid main(String[] args)

{

// I create a new Thread

Thread newThread =new Thread(new secondClass());

newThread.start();//run second thread

}}

publicclass secondClassimplements Runnable

{

progressFrame progress =null;

Thread progressBar =null;

publicvoid updateProgressBar()

{

progress.updateProgressBar(int value);//method in class with JProgressBar to update progressBar

}

publicvoid run()

{

/*Here I create third thread which is responsible for showing JProgressBar.

* This class olso implements Runnable

*/

progress =new progressFrame(null,false,getLengthOfTask (),true);

progressBar =new Thread(progress);

progressBar.start ();

updateProgressBar();

}

How can I write code in main method to be able to run other methods inside second method. Code below prevent repaint JProgressBar.

publicclass mainClass

{

secondClass second =null;

publicstaticvoid main(String[] args)

{

// I create a new Thread

second =new secondClass();

Thread newThread =new Thread(second);

newThread.start();//run second thread

}

}

Peter D.

[2902 byte] By [Peter_Da] at [2007-10-3 2:50:57]
# 1

If it is preventing the repainting then it is happening on the event dispatching thread. You are right it wanting to create a new thread. You might find this helpful:

SwingUtilities.invokeLater()

I am kinda confused by your question all the uses of the word second (second method, second thread, second class). If you still need help please rephrase? Thanks

zadoka at 2007-7-14 20:39:48 > top of Java-index,Core,Core APIs...
# 2

Thanks for replay. Sorry for my example. I cannot send you real code because it is too long.

I will try to resolve "second" word:

second in mainClass is an object of secondClass class

secondClass is class called class from mainClass. It is implementing Runnable interface.

Problem lies in creating new thread in mainClass. Because if I first create object of secondClass(called class) and then make new Thread: e.g.

second_object = new secondClass();

Thread newThread = new Thread(second_object)

This code prevent repaint JProgressBar in second class. But if I do this:

Thread newThread = new Thread(new secondClass());

Everything works OK. I could use second example but I must run a few method during newThread work (i cannot call secondClass mathod becuase i should create new object of this class first).

Peter D.

Peter_Da at 2007-7-14 20:39:48 > top of Java-index,Core,Core APIs...
# 3

It seems like there are to many thread being created (even secondClass creates another thread).

I think what you want to do is create the JProgressBar in your GUI and run the whatever task you need to on a separate thread.

You can update the progress bar with a swing timer that would polll the separate thread to see what progress has been made or you could have the separate thread communicate directly to the JProgressBar using the SwingUtilties.invokeLater() method.

zadoka at 2007-7-14 20:39:48 > top of Java-index,Core,Core APIs...