473,797 Members | 3,199 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read write locks and simple synchronization

oll3i
679 Contributor
read write locks

Expand|Select|Wrap|Line Numbers
  1. public class Person
  2. {
  3.   int age;
  4.   rwLockMgr lockmanager;
  5.   public Person()
  6.   {
  7.     age = 17;
  8.     lockmanager = new rwLockMgr();
  9.   }
  10.   public int getAge()
  11.   {
  12.     lockmanager.lockRead();
  13.     return age;
  14.   }
  15.   public void putAge(int newage)
  16.   {
  17.     lockmanager.lockWrite();
  18.     age = newage;
  19.   }
  20.   public void releaseLock()
  21.   {
  22.     lockmanager.releaseLock();
  23.   }
  24.  
  25.  
  26.  
  27. public class PersonSyncOnly implements Runnable{
  28.     int age;
  29.  
  30.       public PersonSyncOnly()
  31.       {
  32.         age = 17;
  33.  
  34.       }
  35.       public synchronized int  getAge()
  36.       {
  37.  
  38.         return age;
  39.       }
  40.       public synchronized  putAge(int newage)
  41.       {
  42.         age = newage;
  43.       } 
  44.  
  45.       public void run() { 
  46.  
  47.               System.out.println(age); 
  48.       } 
  49. }
  50.  
synchronization

Expand|Select|Wrap|Line Numbers
  1. public class PersonSyncOnly implements Runnable{
  2.     int age;
  3.  
  4.       public PersonSyncOnly()
  5.       {
  6.         age = 17;
  7.  
  8.       }
  9.       public synchronized int  getAge()
  10.       {
  11.  
  12.         return age;
  13.       }
  14.       public synchronized  putAge(int newage)
  15.       {
  16.         age = newage;
  17.       } 
  18.  
  19.       public void run() { 
  20.  
  21.               System.out.println(age); 
  22.       } 
  23. }


what would be the use of these classes to show threads working?
May 4 '07 #1
9 1485
JosAH
11,448 Recognized Expert MVP
Normally when you want to read/write from/to a shared resource you apply the
'CREW' idiom: 'Concurrent Read Exclusive Write'. That means you can have
more than one reader but just at most one writer when there are no readers.

The easiest scheduling method is to stick them all in a queue: readers can
leave the queue when there are no writers (but possibly there are other readers)
while writers can only leave the queue when there's noone else around.

kind regards,

Jos
May 4 '07 #2
oll3i
679 Contributor
i thought that i cd share one instance of person among two threads ?
One setting and the other printing the age.
but i dont know how to do the setting part ?
May 4 '07 #3
JosAH
11,448 Recognized Expert MVP
i thought that i cd share one instance of person among two threads ?
One setting and the other printing the age.
but i dont know how to do the setting part ?
So you don't want to be able to set something while that something is being
printed? Just synchronize them:
Expand|Select|Wrap|Line Numbers
  1. class Something {
  2.    ...
  3.    public synchronized void print() { ... }
  4.    public synchronized void setSomething() { ... }
  5. }
... now the two are mutually exclusive actions.

kind regards,

Jos
May 4 '07 #4
oll3i
679 Contributor
but how to show working with threads so one prints the age and the other updates the age ?
May 4 '07 #5
oll3i
679 Contributor
what else do i need to add to my classes to show the difference between locks and simple synchronization ?
May 4 '07 #6
JosAH
11,448 Recognized Expert MVP
but how to show working with threads so one prints the age and the other updates the age ?
Nothing more is needed: when something is printed you don't want it updated
while printing. Synchronization can do that perfectly; don't forget synchronization
is locking, i.e. it's a language feature.

kind regards,

Jos
May 4 '07 #7
oll3i
679 Contributor
so where to use releaseLock() ?
May 4 '07 #8
JosAH
11,448 Recognized Expert MVP
so where to use releaseLock() ?
If we're still talking about my latest example: there is no need to release an
explicit lock, i.e. leaving the synchronized block automagically releases the
monitor (lock) on that particular object.

The entire 'synchronized' method locks the monitor on the object you synchronize.
When you leave the synchronized block/method that same monitor is unlocked
for you.

kind regards,

Jos
May 5 '07 #9
oll3i
679 Contributor
thank you :)
May 5 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1396
by: Meh-Lit Kim | last post by:
Hi all, I know that under Unix/Linux, there is a max number synchronization objects (semaphores etc.) that one can create. I assume that likewise, there is a max number of mutexes and conditional vars that one can create under a threading environment such as when using Pthreads under Solaris. Now, each java object provides the Monitor functionality, and I
0
1694
by: Rob | last post by:
Hi, do you know any any Java implementation of an improved read-write lock that can give three kind of locks: - Read - Write - Promotable Read The read and write locks are as usual. The promotable read lock can be used when you have to do a lot of
4
40282
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the user commits. But in Informix there is this thing called ISOLATION LEVELS. For example by setting the ISOLATION LEVEL to DIRTY READ, a user will read dirty data, i.e. the last uncommited updated value of a field by some other user. Is
2
3031
by: Matt | last post by:
Hello, In my application that I'm writing in C++ (and maybe Java), specifically the cygwin flavor of C++, I want to be able to open any existing files in FAT32 or NTFS file systems (in Windows XP) for read-only purposes. In particular, I want my program to unconditionally be able to read-only open any file--and be able to read all the contents from said fail--which has been marked with any sort of "exclusionary" lock, including write...
1
2034
by: vadimar | last post by:
Hi, I would like to use database locking mechanism to control access to an external resource (like file system). What I need is 1. an exclusive (write) lock conflicting with any access to the resource (both for read and write) 2. non-exlusive (read) lock conflicting with writes only How this could be done? I'd appreciate any reply. Vadim
2
7658
by: Jürgen Devlieghere | last post by:
Hi, We are creating event-driven multi-threaded applications on a daily basis. To help us solving deadlocks, we implemented a CriticalSection class that does dead-lock detection: an attempt to Enter() the critical section that would cause a deadlock logs the complete deadlock loop (thread / Critical section) and raises an exception. It has helped us a lot in the past. However, to prevent further deadlocks, and to get a higher...
1
1887
by: illegal.prime | last post by:
So I have a container of objects that I don't want to iterate across when I'm modifying it. I.E. I lock on adds and deletes to the container - so that my traversals of it don't result in concurrency issues. However, what do I need to do to allow multiple threads to traverse the container without synchronization/mutual-exclusion - but ensure that synchronization/mutual-exclusion is there when the container is trying to be both changed...
7
13257
by: Igor | last post by:
1. In this topic http://groups.google.com/group/comp.databases.ms-sqlserver/browse_thread/thread/b4a07b516f4a2fcd/cb21516252b65e7c?lnk=gst&q=SET+TRANSACTION+ISOLATION+LEVEL+READ+UNCOMMITTED&rnum=10#cb21516252b65e7c, someone wrote: "I've implemented SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED at the beginning of a number of stored procedures and, then SET TRANSACTION ISOLATION LEVEL READ COMMITTED at the end to minimize the disruption...
9
6828
by: Zytan | last post by:
This program DOESN'T deadlock due to attempt to reenter the same lock! Why? private static object m_lock = new object(); static void Main(string args) { lock (m_lock) { MyFunc(); } }
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.