SwingWorker synchronized method queue blocking or what?

Theoretical question. If I have two SwingWorkers and an outputObject with method

public void synchronized outputToPane(String output)

If each SwingWorker has a loop in it as shown:

//SwingWorker1
while(true) { 
     outputObject.outputToPane("garbage");
}
//SwingWorker2
Integer i=0;
while(true) {
     outputObject.outputToPane(i.toString());
     i++;
}

How would those interact? does the outputToPane method receive an argument from one thread and block the other one until it finishes with the first, or does it build a queue of tasks that will execute in the order received, or some other option?

The reason I ask: I have two threads that will be doing some heavy number crunching, one with a non-pausable data stream and the other from a file. I would like them both to output to a central messaging area when they hit certain milestones; however, I CANNOT risk the data stream getting blocked while it waits for the other thread to finish with the output. I will risk losing data then.

Answers


synchronized only guarantees mutual exclusion. Is not fair, which in practice means that your workers might alternate quite nicely, or the first one might get precedence and block the second one completely until finished, or anything between.

See Reentrantlock docs for more about fairness. Maybe you could consider using it instead of synchronized. Probably even better alternative would be using a Queue.


Need Your Help

Unicode ☰ hamburger not displaying in Android & Chrome

android html unicode font-face hamburger-menu

I noticed that my hamburger menu icon has stopped displaying in Chrome for Android and also, in the Android browser now I simply get the missed icon image. It still works in all desktop browsers, in

Constrain text fields so that they have even space between them and fill the whole screen

ios cocoa-touch autolayout nslayoutconstraint

I'm building an app that is supposed to be able to run on just about any iOS device that is running iOS8+. The issue is that means I have to support many different screen sizes. This is easy for

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.