Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
synchronize variable by lock
Stefan Müllerheim
Greenhorn
Posts: 12
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi, i try to synchronize the access to a variable and wrote something below. is that correct? see anybody any fault?
import java.util.concurrent.locks.ReentrantLock; public class d { private final ReentrantLock l1 = new ReentrantLock(); private int a1 = 0; public int get() { int a2; l1.lock(); a2 = a1; l1.unlock(); return a2; } public void set( int a3 ) { l1.lock(); a1 = a3; l1.unlock(); } }
less is more.
Henry Wong
author
Posts: 23959
142
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
BTW, there is no need to create a new local variable...
public int get() { try { l1.lock(); return a1; } finally { l1.unlock(); } }
Henry
Books:
Java Threads, 3rd Edition
,
Jini in a Nutshell
, and
Java Gems (contributor)
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
GC
access a private member
Garbage Collection
Why we need constructor in Abstract Class ??
Garbage collection query
More...