J
james
Hi,
I have a method where a Thread needs to complete a task independently
of another task outside my Thread on the main Event Dispatching Thread.
However, I don't want to leave the method goTask() until the Thread t
completes.
I am not sure if this is possible without blocking the Event
Dispatching Thread and if it is possible please can someone help me.
The test program below demonstrates my problem (I hope) with the use of
System.out's.
Essentially, I don't want to get to the:
System.out.println("I don't want to get here until the thread t has
finished.");
before:
System.out.println("About to finish Thread");
has been output.
I hope this makes sense and someone can assist me here.
Thank you,
James
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Sync extends JFrame{
/* Swing components */
protected JPanel contentPane;
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Sync s = new Sync();
s.setVisible(true);
}
catch(Exception e){
e.printStackTrace();
}
}
public Sync(){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jbInit();
this.setSize(new Dimension(100,100));
Dimension frameSize = this.getSize();
this.validate();
this.setLocation((dimension.width - frameSize.width) / 2, 0);
}
public void jbInit(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
this.setTitle("Simple Test");
JButton jb = new JButton("Go Task");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
goTask();
System.out.println("I don't want to get here until the thread t
has finished.");
}
});
this.contentPane.add(jb, BorderLayout.CENTER);
}
public void goTask(){
Thread t = new Thread(new Runnable() {
public void run() {
for(int i=0;i<10;i++){
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("About to finish Thread");
}
});
t.start();
System.out.println("This needs to be done seperatly to the
thread.");
}
}
I have a method where a Thread needs to complete a task independently
of another task outside my Thread on the main Event Dispatching Thread.
However, I don't want to leave the method goTask() until the Thread t
completes.
I am not sure if this is possible without blocking the Event
Dispatching Thread and if it is possible please can someone help me.
The test program below demonstrates my problem (I hope) with the use of
System.out's.
Essentially, I don't want to get to the:
System.out.println("I don't want to get here until the thread t has
finished.");
before:
System.out.println("About to finish Thread");
has been output.
I hope this makes sense and someone can assist me here.
Thank you,
James
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Sync extends JFrame{
/* Swing components */
protected JPanel contentPane;
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Sync s = new Sync();
s.setVisible(true);
}
catch(Exception e){
e.printStackTrace();
}
}
public Sync(){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jbInit();
this.setSize(new Dimension(100,100));
Dimension frameSize = this.getSize();
this.validate();
this.setLocation((dimension.width - frameSize.width) / 2, 0);
}
public void jbInit(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
this.setTitle("Simple Test");
JButton jb = new JButton("Go Task");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
goTask();
System.out.println("I don't want to get here until the thread t
has finished.");
}
});
this.contentPane.add(jb, BorderLayout.CENTER);
}
public void goTask(){
Thread t = new Thread(new Runnable() {
public void run() {
for(int i=0;i<10;i++){
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("About to finish Thread");
}
});
t.start();
System.out.println("This needs to be done seperatly to the
thread.");
}
}