Tomasz Wilk

Greenhorn
+ Follow
since Jul 13, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tomasz Wilk

Well, ignore my previous reply... I was wrong, you are right
I am not quite shure should it be -1...
byte is ofcourse 8 bits and is signed so casting 0xFF on it will result in
having -1. But basically 0xFF means all bits as "1". That would be -128.
I am using this value in my assignment.
Should I used -1?
I find them usefull too.
But I don't agree with one thing. ConcurrencyTest fails if a read method throws exception. It is possible and legal to get this if a record was previously deleted. Therefore it can be considered as invalid (deleted) record and i.e. RecordNotFoundException has to be thrown.

Regards,
Tomek
Hi I have a problem with create method. I have many complex solutions but none simple What I want to do (am doing) is I have a map in data class
containing deleted rows. In create method I check if there are any rows which I can reuse. If there are, I take one. If not I extend a database size. I lock a record create it and unlock it. I decided to allow only one
creation thread at a time - method is synchronized on createMutex object.
Here comes a problem - I dont want to use this createMutex. But if I will not use this there can be a problem when there are no row ids to be reused.
Both threads will extend a database. If second will start writting first it will write far outside the file and exception will be thrown. Anybody has idea how to avoid this problem?
Yep, I admit that it is complex and probably don't compile as I just created in a text editor as a general idea.
But:
"You say you wait on a single row, which is false, because you synchronize the access to the whole cookies collection, therefore, while you get synchronize access to this collection no other object, irrespective of the row trying to lock, will get access to it."
You are correct - I am synchronizing on cookies map. But see what is happening there exactly. This synchronization is to only add/remove something from/to cookies. Should I allow multiple threads access the map concurrently? If the map is synchronized internally I would agree... but generally adding/removing elements should be synchronized. Don't you agree?

I know a very simple solution - just to make lock/unlock methods synchronized and wait/notify if a row is currently in use/not in use.
But it will not lock on a row actually. It will lock on a Data object which is... well... not optimized solution... while still being simple. Or... do you have any other solution in mind?

Regards
1. can ant be used to produce a submission.jar? Is there any name required for submission.jar? (I know - it is to contain runme.jar)
2. how do you think? should read be between lock and unlock? i.e. every time we search for rows we have to read them to check if they match query.
I think every row should be processed in locked code so the currently processed row will not be changed by update or delete
3. Data class testing: I created some JUnit tests which just run a lot of reading/writting threads. Do you think for automatic testing that is sufficient for threading?
Hi, what do you think about below lock/unlock code? I know that solution used should not be complex, but... I think my solution would wait only on a row, not on a whole Data object...
cookies - mapa <Integer, Long>
mutexes - mapa <Integer, Object>
mutex - obiekt

public long lock(int rowNr) {


boolean isOk=false;

Long cookie = null;

synchronized(mutexes) {

mutex = mutexes.get(rowNr);

if(mutex == null) {

mutex = new Object();

}

mutexes.add(mutex);

}

synchronized(mutex)

{

while(!isOk)

{

synchronized(cookies) {

isOk = (cookies.get(rowNr) == null);

}

try

{

if(!isOk) {

mutex.wait();

}

} catch (InterruptedException e)

{

log(Level.SEVERE, "InterruptedException during lock");

//poslanie wyzej

}

cookie = generateCookie();

synchronized(cookies) {

cookies.add(cookie);

}

}

}

return cookie;

}


public void unlock(int rowNr, long cookie) {


Long cookie = null;

synchronized(cookies) {

cookie = cookies.get(rowNr);

}//sprawdzenie czy wartosc cookie jest poprawna

synchronized(mutexes) {

mutex = mutexes.get(rowNr);

}

synchronized(mutex) {

synchronized(cookies) {

cookies.remove(rowNr);

}

mutex.notify();

}

}
well, yeah, but the method signature is lock(int recNo) - I would understand that the single record is to be locked, not all of them...
... be generated using ant? Anybody knows how to name main jar which is to be deployed?
Is there any neat way to check in java if something is an url or not? I implemented verifiers which can be used for text-fields... but it seems that simple regex verifier will not do in this case.
Hi All,
I finished writting my application, I started to check it out... and I dont like my threading.
First I wanted to use synchronized + wait and notify. There should be waiting on map of cookies and on seperate value later on. However I read much about locks and I love idea of having ReentrantReadWriteLock.
For every created row I have a lock (just reentrant one). So every row can be locked separately which doesn't stops other threads from working on different rows. My lock() looks like this:
public long lock(int id) {
//I use mainLock to lock create operation as atomic one (I threat it as //write operation)
Lock readLock = mainLock.readLock();
readLock.lock();
Lock lock = null;
synchronized (this) {
lock = locks.get(id);
if (lock == null) {
lock = new ReentrantLock();
locks.put(id, lock);
}
}
lock.lock();
long cookie = generateCookie();
synchronized (cookies) {
cookies.put(id, cookie);
}
return cookie;
}

The problem for me is a create/delete method as they change number of rows... I guess it is not a problem for reading and finding - in worst case scenario some rows will just not be read. But for writting it is a problem because we can overwrite previous value... My delete doesn't phisically delete a row (it only marks it as not available).

So finally, what do you think about such concept? Could you name a book I coud use to polish my threads knowledge?