简体   繁体   中英

java concurrency condition opportune usage

In the application when the "create" button is pressed another subform is popped.The main form waits till the subform is filled and the submit button in the sub form is clicked,after which it procures the data entered in the subform for further processing.

The problem I am confronting is the program hangs after clicking the "create" button,subform is displayed though.

waitTillFilled is the condition used in the application.

Action Listener in the main form

if(e.getSource()==create)

{  try
     { 
    lock.lock();
    try
    {

     model=(DefaultTableModel)table.getModel();
     Form newForm=new Form();
         System.out.println("Waiting to fill the sub form");
     waitTillFilled.await();
     System.out.println("Waiting done....");
     Vector<String> newData=newForm.returnFields();
     System.out.println("added row is "+newData);
     model.addRow(newData);
     System.out.println("Table created"+data);
    } 
    finally
    {
     lock.unlock();
    }
}
catch (InterruptedException e1)
{
    // TODO Auto-generated catch block
    e1.printStackTrace();
    Thread.currentThread().interrupt();
}

}

Action Listener in the sub form

if(e.getSource()==submit)
    {
        String n,p,em;
        n=name.getText();
        p=phone.getText();
        em=email.getText();
        rowData=new Vector<String>();
        rowData.add(n);
        rowData.add(em);
        rowData.add(p);
        System.out.println("added row is "+rowData);
        waitTillFilled.signal();

    }

You're trying to re-invent the wheel here. You should just use a JOptionPane to create a modal dialog that returns some user entered input. See http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

The await() is transfered from the UI thread to a blockingMethod() which spawns a new thread to wait and collect the data from the sub form when signalled.

 Runnable r2=new Runnable(){

    @Override
    public void run() 
    {

        try 
        {   lock.lock(); 
        System.out.println("Lock acquired in blockingMethod");
         System.out.println("about to wait");
         waitTillFilled.await();
         System.out.println("Waiting done....");
         Vector<String> newData=returnFields();
         System.out.println("added row is "+newData);
         model.addRow(newData);//add row
         System.out.println("Table created"+data);
            //invoke setName after reaquiring lock
            lock.unlock();
            System.out.println("Lock released in blockingMethod");
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }};

The actionListener in sub-form is edited to

             lock.lock();
            System.out.println("Lock acquired in actionPerformed");
            try
            {
             System.out.println("Waiting to fill the sub form");
             model=(DefaultTableModel)table.getModel();
             Form newForm=new Form();
             lock.unlock();
             blockingMethod();
            } 
            finally
            {
             lock.unlock();
             System.out.println("Lock released in actionPerformed");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM