aspose file tools*
The moose likes Threads and Synchronization and the fly likes sharing a boolean variable between main thread and the thread for action listener... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "sharing a boolean variable between main thread and the thread for action listener... " Watch "sharing a boolean variable between main thread and the thread for action listener... " New topic
Author

sharing a boolean variable between main thread and the thread for action listener...

Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 63

Hi,

I am reading one text file by comparing each line with a specific string and printing them in console.

I am also getting one input, i am waiting for a JFrame to be closed before printing all the lines.

I'm using a window listener to update a boolean flag when the JFrame is closed. Until that flag is updated to true i am looping using while indefinitely in main method.

Once the execution is out of the while loop, after the JFrame operation has updated the boolean variable, the rest of the code is not acting the same way as expected.

Is this due to some synchronisation issue. ? I am not using any input from the JFrame for now, because i just wanted to test if this works fine before using it.

Please find the code below:



Here if I comment out the code between Swing START and Swing END, then the program works just fine, all the text i wanted from inputText file gets printed in console. Now If make the main method wait using that while loop, its getting bad. Only half or even less of the expected text is being read from the file and printed in console.

Though here I'm not using any input from that JList for reading the file, I might use it later.

I can feel that its somewhat a bad logic to use that boolean flag, but whats wrong here?

Thanks.
Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 63

sorry for wasting a thread. I figured it out what caused the problem. I had both window closing adapter active and the default close operation of JFrame set to EXIT_ON_CLOSE.

When i commented out the exit_on_close line, it worked as i expected.

Sorry for asking question and answering myself.

Still I dont know whats happening behind the scenes, when having both the window closing listener and default close operation. I would like to know that if anyone is willing to shed some light on that. Why it worked correctly when having just window closing adapter alone? why having both the options active for closing the frame affected the main thread?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Mohamed Iqzas wrote:Sorry for asking question and answering myself.

That's not a problem. Thanks for posting the answer. We can all learn from that now

Still I dont know whats happening behind the scenes, when having both the window closing listener and default close operation. I would like to know that if anyone is willing to shed some light on that. Why it worked correctly when having just window closing adapter alone? why having both the options active for closing the frame affected the main thread?


When you make use the EXIT_ON_CLOSE, it is just like putting a System.exit(0); line in your code (and letting it get called at some point after the Windows closes). When you call System.exit() all code execution stops, and the JVM begins to shut down. Therefor whatever code you have that is supposed to execute after that command will not be executed. In your cases the System.exit(0) is being sent when the window closes, and then you wait for the window to close and do some other stuff. The other stuff won't finish because the System.exit() kicks in and stops it.


Steve
Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 63

Thank you. That information is really helpful. So to close the JFrame and at the same time continue the program execution, is it fine to dispose the frame by calling frame.dispose()?

will dispose() method also cause the JVM to exit? I just need to read a set of options from the file. and then display the list in a GUI for the user to select multiple options from that list.

So I used JList to display the options in a JFrame. Is there any better solution for this requirement?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Mohamed Iqzas wrote:Thank you. That information is really helpful. So to close the JFrame and at the same time continue the program execution, is it fine to dispose the frame by calling frame.dispose()?

will dispose() method also cause the JVM to exit?

You should read the API for the JFrame's dispose() method (it is inherited from the Window class). Each time JFrame (or other core Java classes) are presented in the forum they get underlined - indicating a link. If you click the word it will take you to the API. So click on JFrame and then find the dispose() method and see what it does.

I just need to read a set of options from the file. and then display the list in a GUI for the user to select multiple options from that list.

So I used JList to display the options in a JFrame. Is there any better solution for this requirement?


Sounds ok to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sharing a boolean variable between main thread and the thread for action listener...