Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java GUI thread problem

Status
Not open for further replies.

rvy2k

Programmer
Jun 29, 2001
39
0
0
US
Hi all,

I have a JFrame that implements Runnable. This creates a thread that does some work, While it is doing the work, I would like the JFrame/Thread to wait. The problem is that the JFrame just freezes. I tried multiple things Thread.join, Thread.wait, monitor with synchronized method unsuccessfully.

Below is a simpler code snippet that has the same problem.

Can somebody help?
Thanks.

//// Frame Class //////////////////////////////////////////////////////

public class TestFrame extends javax.swing.JFrame implements Runnable {

Thread thread;

/** Creates new form TestFrame */
public TestFrame() {
thread = new Thread(this);
initComponents();
this.setSize(120,60);
this.setVisible(true);
}

private void initComponents() {
panel = new javax.swing.JPanel();
button = new javax.swing.JButton();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

panel.setLayout(null);

button.setText("run thread");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});

panel.add(button);
button.setBounds(0, 0, 120, 26);

getContentPane().add(panel, java.awt.BorderLayout.CENTER);

pack();
}

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("starting");
TestThread t = new TestThread();
while(!t.isDone) {
try {
thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("done");
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public static void main(String args[]) {
new TestFrame();
}

public void run() {

}
// Variables declaration - do not modify
private javax.swing.JPanel panel;
private javax.swing.JButton button;
// End of variables declaration

}

//// Thread Class //////////////////////////////////////////////////////

public class TestThread implements Runnable {

Thread thread;
boolean isDone = false;

public TestThread() {
thread = new Thread(this);
thread.start();
}

public void run() {
for(int i=0;i<10000;i++) {
int n = 0;
for(int j=0;j<100000;j++) {
n += j;
}
}
isDone = true;
}

}
 
Hmm, have you tried creating the Thread in your main class, in the :

private void buttonActionPerformed (java.awt.event.ActionEvent evt) {
System.out.println(&quot;starting&quot;);
TestThread t = new TestThread();
while(!t.isDone) {

such as
TestThread t = new TestThread();
Thread kicker = new Thread(t, &quot;Thread 2&quot;);
kicker.start();

plus also looks like your code will take awhile to execute in that for loop, how long are you waiting? Given that your code perhaps executes at 1 ns per interation of the inner loop, it would still take roughly 17 minutes. I'd also keep track of that loop and print out the variable's i and j in there, making sure the thread isn't being interrupted, calling run again thus restarting the loop. If it is, I'd suggest a synchronized run method.

JavaDude32
 
Err, 17 mins with a microsecond, 10 seconds with a nanosecond. Oops.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top