Java -- thread control, thread security, thread synchronization, thread waiting, thread Interaction

Source: Internet
Author: User

Multiple processes each process has a separate memory space of 0 ~ The 4G Single-core system divides all cpu time into the same time slice. the kernel is responsible for scheduling the thread. All threads inside the process share the memory of the process, so that a process can execute multi-segment code simultaneously. java the jvm thread scheduler is responsible for scheduling the executable queue. Waiting for the queue. The jvm in linux does not implement the thread priority. In windows, this function is implemented. Do not use the priority or any method to change the thread execution sequence. the thread execution sequence in java must be java. lang. instance of the Thread object extends Thread implements Runnable // The implementation inherits other objects to override the run method. The start Thread must call the start method. One Thread can only call the start method once. If you want to start one Thread repeatedly, the object must be instantiated again to sleep the current Thread. sleep (time); Unit: 1 s = 1000 ms (ms) 1 ms = 1000 microseconds (microseconds) implement one thread, each second is executed once, if the loop variable is a multiple of 10, the thread control start () starts the thread interrupt () to interrupt the thread and wake up the sleeping thread to check whether the current thread is interrupted isInterrupted () only check the interrupt mark interrupted () to check and clear the interrupt mark Thread. sleep () Thread sleep, clearing the current interrupt mark Thread. currentThread () Get the current thread object getPriority () Get the thread priority setPriority () modify the thread priority. linux does not implement it, which may easily cause a deadlock stop () stop suspend () suspend destory () resume () thread-safe thread-competing thread synchronization: resources are consumed. Each time a thread enters the code block, the lock is obtained first, the synchronization method synchronized is used until the end of the code block. Each object in the synchronization code block contains a lock synchronized (object) {}, indicating that the method can only be synchronized by one thread at a time. this is equivalent to synchronized (this) {} deadlock generation A --> key1 key2 B --> key2 key1 a B thread may lead to deadlocks. Avoid deadlock. The order of obtaining locks and Releasing locks is as consistent as possible. A --> key1 key2 B --> key1 key2 A --> key2 key1 B --> key2 key1 mutex lock ReentrantLock tryLock (times, timeUint) Wait for the specified time. If the lock is obtained within the specified time, true is returned. Return false unlock () to release the lock. read/write lock: Use ReentrantReadWriteLock lock to read/write data. readLock (); get the read lock. writeLock (); get the write lock. readLock (). lock () lock. writeLock (). lock () lock. readLock (). unlock () lock. writeLock (). the unlock () read lock is not released, the write lock is not allowed to be released, and the read Thread wait is not allowed: it does not belong to the Thread Object and belongs to the Object wait () notify () wake up one of the waiting threads notifyAll () Wake up all the waiting threads a c a --> C. wait () A object waits on the C object, and the synchronized C object must wait for notify to wake up thread interaction IO operations, and the stream PipedInputStream PipedOutputStream

Related Article

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.