• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to: convert "guarded block" into "lock objects"?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear guys,

Below is the progam I've been writing. It has two threads, one (PanelView) is for displaying the data in the file, and one (DataCreator) is for writing the data into the file. In the middle there is an object to synchronize the two threads. The object has two methods, to read and to write data. To synchronize, right now I uses "guarded block" to toggle between reading and writing the data.

My question is how to convert this "guarded block" style of synchronizing into "lock object" style? I means, "the Lock.trylock() and Lock.unlock()" style of synchronizing. I've tried to convert it myself, but noluck so far. I am sorry if my phrase is unclear or confusing, but I studied threading mostly from sun's java tutorial.

Why I need to convert my synchronizing object? Well, firstly I want to study like the rest of the folks here. Secondly, I think that by using "lock object" the synchronizing method will be more effective since the method doesn't wait(). Furthermore, the code line can be reduced.

Thank you for your attention.

And this is the code:

 
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,synchronizing each class is the pargmatic solution.

Am still curious about the tool.
Is there any tool which high-lights blocks where threads interleave,given the code?
 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, but I am not quite sure what you mean.

blocks where threads interleave


But if you're asking about where the threads interleaves, then it is in the ScanDataPool class.

The readData and writeData method is both synchronized method. The mehod will wait according to the boolean variable refreshed. Please note that you have to toggle the boolean variable and at the end of the method you have to call notifyAll to tell the waiting method to go on.

I am sorry if my explanation is inadequate.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic