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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

threading waiting to finish 1

Status
Not open for further replies.

tictaclam

Programmer
Jan 8, 2004
47
US
Hi i'm new to threading and i'm having a difficult time. I have a main program and if a certain event occurs a new dialog pops up. I need the rest of the program to wait for the dialog to finish before it continues. Here's what i have started but it's not working and i'm not sure what to do.
myThread.java
Code:
 public class myThread extends Thread
{
 private myDialog new_reason_;

 public myThread()
 {
   new_reason_ = new myDialog();
 }

 public void run()
 {
   System.out.println("running");
   String reason = new_reason_.display();
   System.out.println("done");
 }
myDialog.java
Code:
public class myDialog extends JFrame 
{
  private JButton ok_;
  private JTextField reason_name_; 
  private Thread thread_;
  private Reason theReason_;

  public myDialog()
  { //set up dialog   }
  
  private void registerListeners()
  {
    ok_.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
	theReason_.setReason("Test");
      }};
  }

  public String display()
  {
    theReason_= new Reason();
    this.setVisible(true);
    return theReason_.getReason();
  }
Reason.java
Code:
public class Reason 
{
  private String reason_;   
  
  public Reason()
  {
    reason_ = "";
  }

  public synchronized void setReason(String v)
  {
    synchronized (this) {			
      notify();
    }
    reason_ = v;
  }
	 
  public synchronized String getReason()
  {
   try{
     System.out.println("waiting...");
     wait();			
   }
   catch (InterruptedException e)
   {}
   System.out.println("done waiting..");
   return reason_;
  }
}

main.java
Code:
  ...
  myThread thread = new myThread();
  thread.start();
    	
  JOptionPane.showMessageDialog(null, "finished");
  ...
i don't want 'finished' to be displayed until the thread is done getting the reason Thanks
 
Use Thread.run() instead of Thread.start()

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks that definitely made some sort of difference. But the problem now is that everything freezes and myDialog didn't finish loading first, so i can't hit 'ok' to reactivate the thread. Any ideas?
 
to restart the thread i need to have the user type in a name on the dialog and hit ok. I played with the code and i can make the dialog finish loading but it's frozen when the thread waits...is there some way i can make the rest of the program freeze but still allow this one dialog to be able to be used?
 
Maybe I'm wrong, but using the run method you don't do any class of multithreading, ¿right?

If that's true you can just avoid the Thread thingie

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top