Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is a safe thread?  RSS feed

 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey

have been reading over a few posts and thread saftey has poped up a lot....

what is a safe thread??

i have a multi-threaded app that i have been working on and would like to know if it is inherantaly safe or not...

on EDT i can start different threads depending on what i want to run so i gond hang the GUI and what will be run at that given time, then i have a special waterfall thread that starts a chain of single threads to run one after the other is this safe??

any info will be great thanks
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread safety is not about threads being safe, it's about objects being safe from being modified by multiple threads at the same time.

Without proper synchronization, if two different threads access the same object this may lead to the object ending up in an invalid state. For example, the body of ArrayList.add:
Let's focus on that "elementData[size++] = e" line. That consists out of three operations:
1) get the current value for size
2) increment size
3) set the element at the specified index (old value of size)

Since the method is not synchronized, two threads X and Y can have any interweaving of these statements. Ideally, first X executes 1-3 and then Y executes 1-3. But what if the following occurs (assumption: size is 5 when called)
X.1 (current value is 5)
Y.1 (current value is 5)
X.2 (size is 6)
X.3 (element 5 is set)
Y.3 (size is 7)
Y.3 (element 5! is set)

So the size is increased by 2 but only one element is effectively added; the addition of thread X is overwritten by the addition of thread Y.


Now, back to Swing / AWT and the EDT. All user interface related code, both querying and modifying, should be done on the EDT. EventQueue.invokeLater, EventQueue.invokeAndWait (with "aliases" SwingUtilities.invokeLater and SwingUtilities.invokeAndWait) and SwingWorker's process and done methods are ways to execute code on the EDT from another thread (SwingWorker's execute method starts a new thread). You should definitely use these when using multiple threads and Swing / AWT.
 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow thanks way over my head but i get the jist of it....

i understand about invokelater running on EDT, i have the code below creating a new thread of its own... is this proper coding?? or should i run that code but on the EDT doInBackground() as i dont want to hang the GUI??

then from the EDT or the GUI i am using at the time i run



 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) read When Runtime.exec() won't

2) the only parts that are wrong are the calls to pauseButton.getText() and pauseButton.setText(...). These are now run in the background thread and should run on the EDT. The getText() call can be done before you fire the thread, where you pass the value to the constructor of the thread which will set its private field (note: add the field, change the constructor). The setText(...) can be done using invokeLater:
 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many thanks will get right onto it...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!