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;
}
}
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;
}
}