If too many locks are marked, the thread waits for other threads to release the lock flag

Source: Internet
Author: User

If the lock tag is too large, the thread waits for another thread to release the lock token without releasing its own lock tag for other threads to run. is a deadlock.

The deadlock problem is resolved through communication between threads.

The inter-thread communication mechanism is actually the coordination mechanism.

The space used by inter-thread communication is called the waiting queue of an object, and the queue is also the space of the object.

Another wait () in the object class, in the running state, calls Wait (), which means that the thread will release all of its lock tokens while entering the waiting queue for the object.

The status of the wait queue is also blocked, except that the thread frees its own lock tag.

Notify ()

If a thread calls the object's notify (), it is a thread that notifies the object to wait for the queue to dequeue. Enter the lock pool. If you use Notifyall (), notify all threads in the wait queue to be queued.

1  Packagetomtexts;2 ImportJava.io.*;3  Public classtomtexts_35 {4 5      Public Static voidMain (String args[])6{bytebuffer[]=New byte[128];7System.out.println ("Please enter data, save to file Test.txt after carriage return");8System.out.println ("Enter # then exit!") ");9         Try{TenFileOutputStream f=NewFileOutputStream ("Test.txt"); One              while(true){ A                 intn=System.in.read (buffer); -                 if(buffer[0]== ' # ') Break; -F.write (buffer,0,n-1); theF.write (' \ n '); -             } - f.close (); -}Catch(IOException e) +             { - System.out.println (e.tostring ()); +             } A     } at  -}

If too many locks are marked, the thread waits for other threads to release the lock flag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.