Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I cannot get to synchronize a variable  RSS feed

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my main program. I need to do run behave in the way what I am trying to do....



//SJTruck



//Lorry

 
Salil Wadnerkar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to explain what is that you are trying to do. Also, reduce your code to a small snippet where the problem lies.
 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Wadnerkar wrote:You need to explain what is that you are trying to do. Also, reduce your code to a small snippet where the problem lies.


 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salesFromTruck1 when is added to in the Event Dispath Thread, and you're reading from it in another thread.

You need to synchronize access to this variable. Not only that, but it's a really bad idea to make your variables static.

Why is your UI Runnable?. That makes no sense and leads to incredibly brittle code. If you need to have things done concurrently, submit tasks to an executor service. Even better, if they are Swing background tasks, use a SwingWorker instead.
 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:salesFromTruck1 when is added to in the Event Dispath Thread, and you're reading from it in another thread.

You need to synchronize access to this variable. Not only that, but it's a really bad idea to make your variables static.

Why is your UI Runnable?. That makes no sense and leads to incredibly brittle code. If you need to have things done concurrently, submit tasks to an executor service. Even better, if they are Swing background tasks, use a SwingWorker instead.


How can I synchronize salesFromTruck1?
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I'm not convinced that you need to. Why are you using background threads at all? Incrementing a counter isn't that much of a time-consuming operation.

Some general remarks:

  • Why are all of your classes Runnable? None of them represent a task.
  • Why doesn't your UI use layout managers? Using null leads to terrible layout issues.
  • SJTruck and Lorry seem to be doing the same thing. Are you sure you need two classes?
  • Your comboBox ActionListener doesn't have to look up the source of the event. It can be proven that cb must always be the same as comboBox.
  • whichCommit is not a useful variable. It's just an alias for your comboBox's current selection. In fact, your comboBox doesn't even need an ActionListener at all.
  •  
    Stephan van Hulst
    Saloon Keeper
    Posts: 6980
    110
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another big one: TruckUI extends JFrame, but also contains a JFrame. Don't let it extend JFrame.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!