Concurrency problem collation in Java

Source: Internet
Author: User
Tags cas

1. There are several ways to implement a thread in Java?

Using the Runnable,callable,thread or thread pool

2. How do I stop a running thread?

You can use a running thread to support a thread break, usually to define a volatile state variable, to read the variable in the thread that runs it, to modify the variable in other threads

What's the difference between 3.sleep and wait?

The sleep method suspends the executing thread for a specified period of time, but does not release the lock. The wait method is to let the current thread wait until another thread calls the object's notify or Notifyall method. The wait method frees the lock so that other threads have a chance to occupy the lock

What is the difference between 4.notify () and Notifyall ()?

Notify is a random wake-up of a thread waiting for a resource, into the ready queue waiting for the CPU to dispatch, Notifyall is waking all, into the ready queue waiting for CPU scheduling

5. Introduction of the next daemon thread

There are two types of threads in Java: User thread, Daemon thread (daemon thread). The role of Daemon is to facilitate the operation of other threads, such as the garbage collection thread, which is a very competent guardian. The only difference between user and daemon is that the virtual machine leaves: If the user thread is all out of operation, only the daemon thread is present, and the virtual machine exits.

6. Optimistic lock and pessimistic lock understanding and how to achieve, what is the way to achieve

Optimistic lock, each operation without locking but assume that there is no conflict to complete an operation, if the conflict failed to retry, until successful

A pessimistic lock causes all other threads that need to be locked to hang, waiting for the thread that holds the lock to release the lock.

Optimistic locks can be implemented using the Volatile+cas primitive

Pessimistic locks can be used with the Synchronize and lock

7. How do I make a program run concurrently and eventually summarize the results?

Use Cyclicbarrier to perform a summary of the results on multiple threads at multiple gates; Countdownlatch report results to the bus after each thread finishes execution

8. How to properly configure the Java thread pool? such as CPU-intensive tasks or IO-intensive tasks, how large should the basic thread pool be configured?

CPU-intensive tasks can be configured with fewer threads, presumably equivalent to the number of CPU cores of the machine, allowing each thread to perform tasks, and IO-intensive, where most threads are blocked, so multiple configuration threads are required, 2*CPU number of cores

9. Which concurrent container should be used for multi-read and less-written scenes, and why use it?

Copyonwritearraylist This container is suitable for multi-read and less writing. Read and write are not on the same object. When writing, the array is copied in a large area, so the performance of the write is poor, and the read reference is changed to the object to write after the write is completed.

10. How to implement optimistic locking (CAS)? How to avoid ABA problem?

1.CAS Primitives have three values, one is memory value, one is expectation, and one is write value. When writing without locking, the memory value is read each time, then compared to the expected value, if the comparison fails, repeated reads and the alignment until successful. In the CAs Primitive is an atomic operation, and if the memory value changes when writing, the write value fails.

2. With the parameter version to avoid the ABA problem, when reading and replace the decision version is consistent

11. What scenarios can I use volatile to replace synchronized?

You can use volatile overrides when you need to ensure the visibility of shared resources, synchronized guarantees operational atomicity consistency and visibility. Volatile applies to cases where the new value does not depend on the value

12. What is a reentrant lock (reentrantlock)?

The Reentrantlock is also reentrant relative to the intrinsic lock synchronized, providing higher performance than the intrinsic lock on some VM versions, providing richer locking features such as interruptible locks, waiting locks, equal locks, and non-block structure locking. Using intrinsic locks as much as possible from the code, the VM optimizes the intrinsic locks and the code is maintainable and stable. You can consider Reentrantlock implementation only if you need some features of reentrantlock.

13.ReentrantLock and synchronized comparison. From the Java concurrent Programming combat

1. Why does the Juc frame appear lock?

Reentrantlock is not an alternative to synchronized, but is an optional advanced feature when the built-in lock does not apply.

2. What are the disadvantages of synchronized?

①. Only one condition is associated with a lock, what is this condition? is the synchronized object lock on the target.

②. Synchronized cannot interrupt a thread that is waiting to acquire a lock, that is, when a thread competes for a lock, the remaining unlocked threads can only keep trying to acquire the lock without interruption. This can result in a decline in performance for a large number of competing threads.

3. How do we choose to face Reentrantlock and synchronized?

Compared to lock, synchronized is familiar to many developers, and concise and compact, if the existing program has used the built-in lock, then try to keep the code style unified, try not to introduce lock, avoid the two mechanisms mix, easy confusing, and prone to error.

Lock can be used as an advanced tool in situations where the synchronized cannot meet the requirements, including "timed, polled, and interruptible lock acquisition operations, fair queues, and non-block lock" or if the synchronized is preferred.

Finally, the future is more likely to improve the performance of synchronized rather than lock, because synchronized is a built-in property of the JVM, and he can perform optimizations such as lock elimination optimizations for thread-locked lock objects, eliminating the synchronization of built-in locks by increasing the granularity of locks. If you implement these features based on a class library lock, you are unlikely to do so.

Whether 14.volatile guarantees the visibility of elements in an array

The volatile array is only a reference to the array with volatile semantics, not its elements

Concurrency problem collation in Java

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.