• Post Reply Bookmark Topic Watch Topic
  • New Topic

Let one Thread set a Boolean and edit a ArrayList in another Thread  RSS feed

 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

first of all, there is no Code existing yet, I am only imagining how I'm going to write the code when I'll start doing it.
I've planned to do a very little Game with Java, which leaves me at a Question:

If I do a Loading Screen, I'll need one thread caring for the GUI itself, and then one thread loading everything into ArrayList out of data and turning boolean's to 'true' in Thread1. Imagine it a bit like this:


Thread1 = Set JLabel to 'Loading Level', while-loop to check if Load1 is true
Thread2 = loaded the Level, put everything needed into the ArrayList (in this case the tiles), set Load1 in Thread1 to true
Thread1 = Load1 is true, change JLabel to 'Loading People', while-loop to check if Load2 is true
Thread2 = loaded the People, put everything needed into the ArrayList (in this case the peopleentinitys), set Load2 in Thread1 to true
Thread1 = Load 2 is true, Go through the ArrayList of Level and get the corresponding Tile.png(or whatever) and row them after each other [...]

This is probably not very efficient, but for a beginner it's what I need. I've been thinking of this various possibilities:
1. Let Thread2 write False/False in a text data and Thread1 reads the data every 0,5 seconds to see if it is True/False or True/True and set the boolean
2. Let Thread2 go back into the main() class on the bottom of the stack and change the Data there and let Thread1 check the main() aswell, both Threads beeing started from there.

I've not yet tested both of them, but I'm sure there must be a better way to do this.
I hope I've clearly written what I meant to do, if there are any questions left, just post them!
Oh, and yeah, I've already googled, but I did not quite understand what I found there...

Best Regards,

Joe Degler
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought not to create a thread to deal with GUIs because Swing components are not thread-safe.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, what Campbell said: There is already a Thread to do GUI updating, and what you need to do is run any code which changes labels, and such, in the Event Dispatch Thread. so, if we assume "Thread1" in your scenario is the Event Dispatch Thread then we get to this:

Event Dispatch Thread = Set JLabel to 'Loading Level', while-loop to check if Load1 is true

We never want to put the Event Dispatch Thread into a waiting loop, because that prevents your GUI from being re-drawn, moved around, or responding to the user in a safe manner. So what is usually done is to have your Worker thread (Thread2 in your scenario) cause an event to occur which gets executed on the Event Dispatch Thread when you are ready for the change to occur. There are a couple of tools which make this easier, and the best one for your case is probably the javax.swing.SwingWorker.

The SwingWorker breaks the task into several Threads for you, so you don't have to worry about it. It breaks the task into methods for publishing results to the GUI and performing long-lasting tasks like loading a bunch of resoures. By writing your code in the proper methods you get the inter-thread communication you need. Here is an example:

First, the GUI class, which is a simple JFrame with a JLabel which gets updated with current status. It also holds a method to set the status text.


The second class is the SwingWorker class used to do your work.


Finally, a 'DataModel' class which would hold the two Lists you are filling, and a Runner class which has the main method which starts everything:
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote: [...]


Thank you for giving me the probably most understandable answer ever. I took some looks at the API to figure out what which commands mean, there are still some things I dont know, but I think I'll find out their use while I program.

Even still, Thank you thousand times!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!