FileChannel.lock() question

Hi
(code below)
According to the documentation on the FileLock.lock() method...
" An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first. "
So I would expect whichever thread that gets executed second to wait patiently until a lock on the (0,23) portion of the file can be obtained.
However, I get a "Java.nio.channels.OverlappingFileLockException" when the second thread attemps to acquire a lock on the (0,23) portion of the file.
How can I get a thread to wait until it can acquire a lock on the (0,23) portion of the file can be obtained... and then acquire the lock???
Thanks for your help...
public static void main(String[] args) {
  try{
    final File f = new File("<filename>");
    final RandomAccessFile raf = new RandomAccessFile(f, "rw");
    final FileChannel channel = raf.getChannel();
    class tobj implements Runnable {
      public void run(){
        try{
          channel.lock(0, 23, true);
          System.out.println(Thread.currentThread() + " is done");
        catch(Exception e){
          System.out.println(e);
    new Thread(new tobj()).start();
    new Thread(new tobj()).start();
    //raf.close();
  catch(Exception e){
    System.out.println(e);
}

I have a flat file that is accessed by multiple threads. Speed is of utmost importance. If thread A wants to read bytes 1-50 and thread B wants to overwrite bytes 100 - 150, the program should allow the threads to do this concurrently as the threads will not affect each other. But if thread A wants to overwrite bytes 1 - 50 then immediately after thread B wants to read the same area (bytes 1 - 50), then thread B should wait until a is finished with that area of the file.
Multiple threads should be able to read from the same area at the same time.
But when a thread wants to write to an area of a file, it should have sole control of that area until it is finished.
Any ideas..
I really appreciate you help..
I really appreciate you help..

Similar Messages

  • About session abnormal termination, table locked question.

    Hi all,
    i test some table lock,after i run this statement:
    select * from testt for update nowait;
    i abnormal terminate session, then the table testt will be locking until after 20 minutes,
    i checked some document, the parameter of SQLNET.EXPIRE_TIME = 5 in sqlnet.ora file can release that lock, i set with 5,and then test again, but still wait 20 minutes the lock can release, why?

    I've changed this now to use the URL variable. I am used to using that to go from a results page to a details page where the recordset on the details page is used. I hadn't realised that you could just set a link to:
    <a href="addNomination.php?LodgeID=<?php echo($_GET['LodgeID']); ?>">Add Nomination</a>
    where there is no recordset on a page, just for the purposes of passing the variable. (Yeah, I know - this is probably really basic!)
    I do have one last question on this though - my form action is:
    <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
    And then the link to a NominationAdded page is in the php code at the top of the AddNomination page:
    $ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "NominationAdded.php");
    What would be useful would be to pass the URL variable through again, so that on the NominationAdded page I can have a link back to the same Lodge to add another Nomination without finding the Lodge again.
    But I'm not sure what the syntax would be, as it included some PHP within existing PHP, so not just:
    $ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "nominationAdded.php?LodgeID=echo($_GET['LodgeID'])");
    I assume this must be possible, but not sure of the exact syntax?

  • File locking question

    Hello,
    We're considering using java to write in a new(to us) file system, OCFS2. However, this file system lacks certain features expected for file locking. Our app does not use java.nio.* that much, but it does use it. My question is what exact standards does the JVM require for file locking? Or is it ad hoc?
    If its ad hoc, what sys/func calls are required for locking?
    Thanks,

    The docs are rather specific that all of the functionality for FileLock is platform dependent.
    So you should test it.

  • Explicit Locking Question

    Dear all
    I have a question about <<Developer’s Guide>> Explicit Locking (27-3):
    "In a replicated cache, puts are blocked, but they are not blocked in a partitioned cache. "
    I want to know why "puts are not blocked in a partitioned cache."
    Thanks

    wangjw wrote:
    Dear all
    I have a question about <<Developer’s Guide>> Explicit Locking (27-3):
    "In a replicated cache, puts are blocked, but they are not blocked in a partitioned cache. "
    I want to know why "puts are not blocked in a partitioned cache."
    ThanksThe normal behaviour is that lock does not block anything other than another lock.
    The put-blocking behaviour at replicated caches is an anomaly which may be necessary due to backward compatibility reasons, or possibly a necessity due to the message flow for mutating operations in a replicated cache.
    So the question really would be why puts are blocked in a replicated cache, and I would also be interested in the answer, although I have my guesses.
    Best regards,
    Robert

  • Hashtable locking question

    I see that the put and get methods of Hashtable are synchronized, which is great for multi-thread execution, however, if outside the Hashtable class I use
    Hashtable t = new Hashtable();
    synchronized(t) {
       // loop over the hashtable in here and print out the values
    }is that synchronized block using the same lock as Hashtable's internal one? In other words, if I'm inside that synchronized block am I assured that no other thread can call get() or put()?
    I'm just looking for the proper way to iterate over the Hashtable while assuring myself it won't be modified.
    thanks!!!

    I looked up the SynchronizedMap source and don't see a big difference between it and Hashtable. SM uses synchronized blocks in every function and Hashtable uses synchronized methods. Is their a difference? If it's just a matter of one way being preferred over another let me know.
    Also, I'd still like an answer to my original question about the locks...is the object lock in a synchronized block the same as the class lock on that object's synchronized methods (see my example in post 1)?
    thanks!

  • Trivial Lock Question - nested synchronized instance method call

    Hi there. I have a question surrounding the following code block:
            public synchronized void bow(Friend bower)
                System.out.format("%s: %s has bowed to me!%n",
                        this.name, bower.getName());
                bower.bowBack(this);
            public synchronized void bowBack(Friend bower)
                System.out.format("%s: %s has bowed back to me!%n",
                        this.name, bower.getName());
            }If the bow method is invoked by the currently executing thread that has obtained the lock on the current instance and the
    call "bower.bowBack()" is invoked, what happens? Is the lock released then obtained again by the current thread? or does it hold onto it since the method
    "bowBack" is called within the method "bow"?
    Thank you!
    Regards

    thank you as always mr. verdagen!
    regards

  • Ipad mini auto lock question

    I put the auto lock to 10 mins but when my ipad mini is turned off and i turn it on with the little apple on the screen the screens seems to automatically turn off right after the lock screen appears. Is it normal?? I once had a samsung tab and it was doing that all the time and i realized it was messed up.
    Thank you. Have a nice day.

    My 32 GB iPad 2 has a capacity of 28.3 GB.
    It should be approximately the same for any 32 GB iPad.

  • Record Locking question

    Hi,
    I have the following situation:
    1) A batch process is running within a transaction with intension of commiting the changes at end if the process completes
    2) An online system (internet 3Tier app) issues an UPDATE statement on a record that is locked by the batch process. This UPDATE statement is not in a transaction.
    3) The problem is the UPDATE statement waits until the batch process either performs a COMMIT or ROLLBACK. I want it to return immediately so I can notify the online user.
    How can I have the UPDATE come back rather than wait. I do not really want to issue a SELECT statement with FOR UPDATE NOWAIT just before the UPDATE statement. Thanks in advance.

    Thanks for the reply.
    Normally I would use "select ... for update no wait" especially in desktop type applications. But in the internet world, when dealing with COM objects, ADO, and connection pooling, I cannot issue a start transaction, then issue select with for update nowait, and then show the html form to the user to make changes. The user may sit on the screen for a while. In the meantime, I have a database connection that is being tied up.
    So for record concurrency, we used a simple counter mechanism where any update (internet app, or desktop app) would update the counter. Any on update whe add this counter field to the where clause so if somone else has changed the data, the update would result in 0 rows affected. Normally this works well except in a situation where a desktop app is running a batch process and has not committed yet. In this case the update just waits.
    I know for if I juggle the data around a bit I can issue select for update no wait, then build the update recordset, and issue update, and them commit. I was looking for something that can return the update statement immediately with some sort of ORA error.
    Sorry for the long note.

  • Lifecycle designer 8 Field Locking question

    I have created a Remote access request form that is sent out to people when they need to connect to our network remotely. the form asks them to enter their contact details (Text field) and i have also created a table that asks what server they need access to (drop down list). However i also have text fields next to the drop down list. The user is only supposed to fill out their contact details and server name fields, then send it back to me and i will issue them an ip address, password etc. I will then save the form and send them a copy for reference. What i would like to do is lock down the form so they can only enter data in certain fields, but when it is sent back to me i can fill in the rest of the fields, ( ip address, password fields etc). Choosing to lock down a field after signing isn't any help to me because when they sign it and send back to me i want to be able to enter the rest of the information.
    Is there any possible work around to this.. how will the document know who has full access to it?
    Cheers,
    Dan
    IT Admin
    FEA LTD.

    A couple steps have to be made to have your form expand. Make sure when you save the form, it is saved as "Dynamic" and not "static".
    Also, make sure you click "Expand to fit" under "Height" in the Layout tab.
    If you want everything below your "expandable" section to move down, you will have to make sure all of those parts below are "wrapped" in a subform and the content is set to "flowed". You can wrap a "positioned" subform in a "flowed" subform.
    I hope this is some what helpful and understandable.

  • Database locking question

    Hi all,
    I have a small problem here that I've been trying to resolve for some time and couldn't come with a solution that "feels right".
    i'm working on a finance application where we have a table to
    keep a history of customer's transactions. The problem has to do with three fields:
    - customer's accountid
    - transaction amount
    - forward balance
    the first two are trivial, the last one is calculated on a row insertion: we get the last row for the account, and add the amount value from the new row(one being about to be inserted) the resulting amount becomes the new forward balance for the new row. This is like keeping the per-transaction account balance on a bank statement.
    Now, the problem is that we have several application servers inserting these rows into the same database in parallel. And now what happens is that if two servers are trying to insert rows (for different transactions but for the same acount) in the forward balance calculation logic both processes read the same last (old) row's forward balance and add that to their respective transactions' amounts and then record the new records. The forward balances in both new transactions are now screwed.
    I'm not sure how to fix this problem: I was reading about different database isolation levels and considered using pessimistic locks but all those mechanisms seem to solve problems with updating the same rows - not a combination of reading one row, and creating a new row with that row's result.
    Just to be complete: we are using MySQL (latest) with Hibernate 2.1.6 on Resin application server.
    Thank you in advance for any suggestions/recommendations/advice.
    -Oleg.

    You could move forward balance into its own table. That was, the SELECT will not lock on the original table. Most of the running balance schemes that I have seen take a snapshot as of a given date (say, statement printing date) and record both the timestamp and the amount. Next month, you simply get last month's vaule and timestamp and query on transactions after that timestamp.
    - Saish

  • Layer is Partially Locked question?

    I'm using Windows 7 64 bit but I use Photoshop CS5 32 bit.
    In my Layers Panel every file that I open up has that little icon (Indicates Layer is partially Locked).
      A lot of my functions are grayed out because of this condition. If I duplicate the Layer or put a selection around a layer or make a new layer (Layer 0) all previous grayed out functions now work.
    I uninstalled and reinstall PS CS 5 but still had the same problem. Is there something I can do about this condition?
                    Thank you

    I'm not sure if this is your problem, but every time an image is opened for the first time in Photoshop, there is one layer called Background.  This layer is automatically locked.  You can convert the Background layer into a normal (unlocked) layer by double-clicking on the word Background. You are prompted to give the layer another name (default is Layer 0).

  • Lock Question

    Hello,
    I have the following sentence on a procedure:
    LOCK TABLE ae_bc_trx IN ROW SHARE MODE;
    I need to know how to free the Lock in the same procedure or it liberates itself when I commit a transaction on the table?
    We are on DB 10g.
    Thanks

    http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_9015.htm#SQLRF01605

  • 11g oracle lock question

    I'm trying to understand locking on "other end of FK", i..e. operations on child table in 11g. Database version 11.2.0.2.0
    Table bal_fct has columns(branch id, acct id, cust id, bal dt, bal ccy id, stmt id, bal amt). PK/Unique index on branch id, acct id, cust id, bal dt, bal ccy id, stmt id in that order. All these columns have FKs. No index on these FKs. There is a bitmap index on acct_id.
    An ETL job is doing inserts in bal fct table (child). As I understand, any operation on child table would take RX(mode 3) on parent tables in 11g.
    I queried locks taken. It is taking mode 3 lock on all the dimension tables EXCEPT ACCT(acct_id). Why? I assumed that it will take also mode 3 lock on ACCT also. I have read in other threads that bitmap index really don't count in these discussions.
    Thanks for your help.
    Edited by: rkuppa on May 26, 2013 7:10 AM
    Edited by: rkuppa on May 26, 2013 7:11 AM
    Edited by: rkuppa on May 26, 2013 4:23 PM

    >
    I'm trying to understand locking on "other end of FK", i..e. operations on child table in 11g. Database version 11.2.0.2.0
    >
    Here are two articles about that by Jonathan Lewis
    http://jonathanlewis.wordpress.com/2010/06/21/locks/
    http://jonathanlewis.wordpress.com/2010/02/15/lock-horror/
    >
    Table bal_fct has columns(branch id, acct id, cust id, bal dt, bal ccy id, stmt id, bal amt). PK/Unique index on branch id, acct id, cust id, bal dt, bal ccy id, stmt id in that order. All these columns have FKs. No index on these FKs. There is a bitmap index on acct_id.
    An ETL job is doing inserts in bal fct table (child). As I understand, any operation on child table would take RX(mode 3) on parent tables in 11g.
    I queried locks taken. It is taking mode 3 lock on all the tables EXCEPT ACCT(acct_id). Why? I assumed that it will take also mode 3 lock on ACCT also. I have read in other threads that bitmap index really don't count in these discussions.
    >
    You won't get row level locks when a bitmap index is used.
    Can you edit your post and clarify it a bit.
    You mention 'bal_fct table (child). The table name has 'fct' in it: is this a fact table or a child table?
    And post whether your issue is similar to the one in this other thread from yesterday.
    11g child table update locking
    Some of the info there may help.

  • Entity locking question

    When an entity has a @Version property, is it automatically version checked on flush/commit or lock() must be called in order for it to get this behaviour ?

    Im talking in general, cos i am doing an API to be called from both standalone and server environment.
    What should I do here ?
    Please tell me the general rule..

  • Oracle locking question

    ADDR KADDR SID TY ID1 ID2 LMODE REQUEST CTIME BLOCK
    00000004160D72B8 00000004160D7310 1966 AE 100 0 4 0 499 0
    00000004160D7FD0 00000004160D8028 1966 TO 81148 1 3 0 498 0
    FFFFFFFF7B646EB0 FFFFFFFF7B646F10 1966 TM 79582 0 3 0 23 0
    0000000414E1A040 0000000414E1A0B8 1966 TX 393244 678662 6 0 23 0
    I am getting above locks when i am ussing select for update no wait from oracle forms and it lock the whole table I can not under stand why.
    When issue the same select for update nowait from sql prompt I am getting following
    ADDR KADDR SID TY ID1 ID2 LMODE REQUEST CTIME BLOCK
    00000004160D7798 00000004160D77F0 1997 AE 100 0 4 0 75 0
    FFFFFFFF7B646EB0 FFFFFFFF7B646F10 1997 TM 79582 0 3 0 44 0
    0000000414F002E8 0000000414F00360 1997 TX 655387 838567 6 0 44 0
    and after above when I issue another SQL select for update for different row on same table the second session is:
    ADDR KADDR SID TY ID1 ID2 LMODE REQUEST CTIME BLOCK
    00000004160D71E8 00000004160D7240 1982 AE 100 0 4 0 4123 0
    FFFFFFFF7B645E78 FFFFFFFF7B645ED8 1982 TM 79582 0 3 0 17 0
    0000000414E7DDE8 0000000414E7DE60 1982 TX 589848 710577 6 0 17 0
    Can someone tell me what is
    1. Lock type TO and AE
    2. Why locky type TO is there in oracle forms
    3. why issuing the same select for update nowait from oracle form does not allow any other session sql or from oracle forms to get the lock on row?
    Thank you.

    >
    I sill not able to get what TO lock type is, this was not in the following:
    >
    See the Database reference
    http://docs.oracle.com/cd/E14072_01/server.112/e10820/enqueues.htm#g950013
    >
    D Oracle Enqueue NamesThis appendix lists Oracle enqueues. Enqueues are shared memory structures (locks) that serialize access to database resources. They can be associated with a session or transaction. Enqueue names are displayed in the LOCK_TYPE column of the DBA_LOCK and DBA_LOCK_INTERNAL data dictionary views.
    A resource uniquely identifies an object that can be locked by different sessions within an instance (local resource) or between instances (global resource). Each session that tries to lock the resource will have an enqueue on the resource.
    The Oracle enqueues are:
    TO, Temporary Table Object Enqueue

Maybe you are looking for

  • How to manage my daughter's new phone account

    We just got my 11 year old daughter her first phone (a Samsung S3 Mini). The salesman told us we would be able to use the "Family Base" to control/ turn-on-off her phone, text, web access and email and we would have full control there. Our main goal

  • Caching in plsql

    hi expert, can anyone tell me that how i achieve cashing through plsql code. i can not understand that how i am write a code in plsql. Plz help me. thx & Regds, jaideep awasthi

  • Palm Desktop stops working

    When I am in the calendar and want to go back a month (from April to March) I get an error message.  "Palm OS Desktop Application has stopped working" Here is the problem details Problem signature:   Problem Event Name:    APPCRASH   Application Name

  • Multi-field Interactive Stamp?  Server Accessible?

    I am trying to figure out how to create a custom multi-field stamp that prompts the user to input information into each field (but not required).  I've gotten as far as creating the stamp .pdf file and adding form fields to it, but don't know how the

  • At  first record of innerloop , want to know how many records exists

    How can I know the number of records in INTBSEG for each record of int_maintable before running the loop for that where condition of INTBSEG. Please see below: LOOP AT int_maintable.   Loop at INTBSEG where bukrs = int_maintable-bukrs