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