In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the problems about threads in java". In daily operation, I believe many people have doubts about threads in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions about threads in java. Next, please follow the editor to study!
1), how to share data between two threads.
You can do this by sharing objects between threads, and then evoke and wait through wait/notify/notifyAll and await/signal/signalAll. For example, the blocking queue BlockingQueue is designed to share data between threads.
2), the difference between sleep method and wait method.
Both the sleep method and the wait method can be used to give up CPU for a certain amount of time. The difference is that if a thread holds an object's monitor, the sleep method does not abandon the object's monitor, and the wait method discards the object's monitor.
3) what is the function of producer-consumer model.
The main results are as follows: (1) it is the most important role of producer-consumer model to improve the operation efficiency of the whole system by balancing the generating capacity of producers and the consumption capacity of consumers.
(2) decoupling, which is the role attached to the producer-consumer model, decoupling means that there are fewer connections between producers and consumers, and the fewer connections, the more they can develop independently without mutual constraints.
4) what is the use of ThreadLocal.
ThreadLocal is a method of exchanging space for time. In each Thread, an address-based ThreadLocal.ThreadLocalMap is maintained. If the data is isolated and not shared, there will be no thread safety problems.
5) why the wait () method and the notify () / notifyAll () method are called in the synchronization block.
This is enforced by JDK, and the wait () method and the notify () / notifyAll () method must acquire a lock on the object before calling it.
6), what is the difference between the wait () method and the notify () / notifyAll () method when abandoning the object monitor.
The difference between the wait () method and the notify () / notifyAll () method in abandoning the object monitor is that the wait () method immediately releases the object monitor, while the notify () / notifyAll () method waits for the thread's remaining code to finish executing before abandoning the object monitor.
7), why use thread pools.
Avoid creating and destroying threads frequently. Achieve the reuse of thread objects. In addition, the number of concurrency can be flexibly controlled according to the project by using thread pool.
8), how to detect whether a thread holds an object monitor.
The Thread class provides a holdsLock (Object obj) method that returns true if and only if the monitor of the object obj is held by a thread. Note this static method.
9), the difference between synchronized and ReentrantLock.
Synchronized is the same keyword as if, else, for, and while, and ReentrantLock is a class, which is the essential difference between the two, since ReentrantLock is a class. It provides more flexible features than synchroinized.
(1) ReentrantLock can set the waiting time for acquiring locks to avoid deadlocks.
(2) ReentrantLock can obtain the information of various locks.
(3) ReentrantLock can flexibly implement multi-channel notification.
10) what is the concurrency of ConcurrentHashMap?
The concurrency of ConcurrentHashMap is the size of segment, and the default is 16, which means that up to 16 threads can operate on ConcurrentHashMap at the same time, which is the biggest advantage of ConcurrentHashMap over Hashtable.
11) what is ReadWriteLock?
ReentrantLock is sometimes limited. Locking reduces the performance of the program. Read-write lock ReadWriteLock. Is a read-write lock interface, read-write separation, read lock sharing, write lock monopoly.
12) what is FutureTask?
FutureTask represents a task for asynchronous operations. In FutureTask, you can pass a specific implementation class of Callable, which can wait for the result of the task of asynchronous operation to be obtained, determine whether it has been completed, cancel the task, and so on. Of course, because FutureTask is also the implementation class of the Runnable interface, FutureTask can also be placed in the thread pool.
13), how to find out which thread uses CPU longest in Linux environment
(1), get the pid of the project, ps-ef | grep java
(2), top-H-p pid
Distributed basic theory
1,CAP
CAP theory in distributed domain.
1), Consistency, consistency: data consistency, all data changes are synchronized.
2), Availability (availability), good response performance.
3), Partition tolerance: partition tolerance.
In distributed systems, these three elements can only achieve two points at most, and it is impossible to take all three into account; for distributed data systems, partition tolerance is the basic requirement; for most Web applications, it is the direction of most distributed database products to sacrifice consistency for high availability.
2,Base
1), Basically Available; is basically available, support for partitioning failed.
2), Soft state soft state: the state can be out of sync or asynchronous for a period of time.
3), Eventually consistent: final consistency, as long as the final data is consistent, not always.
3The two core theories of NoSQL movement.
1), BigTable of Google
BigTable proposes a data model to sort and store the data of each column. The data values are distributed on multiple machines according to the range, and the data update operation has strict consistency guarantee.
2) Dynamo uses a different distributed model. Dynamo's model is simpler, it stores data as key for hash. Its data slicing model has strong disaster tolerance, so it achieves relatively loose weak consistency: final consistency.
Design patterns in JAVA I Pot O
1, the adapter pattern is to transform the interface of one class into another interface that is acceptable to the client. This enables two classes whose interfaces do not match and cannot work together. Usually used when a project needs to reference some open source frameworks to work together, there are some interfaces about the environment information inside these frameworks, which need to be referenced from the outside, but the external interfaces may not match. You need the adapter pattern to transform the interface.
There are many such requirements in the Java's Icano class library, which converts strings to byte data and saves them in files and turns byte data into data streams. Specifically, InputStreamReader and OutputStreamWriter are the embodiment of the adapter. InputStreamReader implements the Reader interface and holds a reference to InputStream, which adapts inputStream to Reader. The source role is the instance object represented by inputStream, and the target role is the Reader class.
2, decorator mode
The function is to make the function of the decorated person more powerful, and the way of use before and after decoration remains the same. There are many combinations of different functions in the Java Imax O class library, and these different functional combinations are realized by using the decorator pattern.
Take FileInputStream as an example
Because the java I Pot O library requires a variety of combinations of performance, if these performance is achieved by inheritance, then each combination requires a class, resulting in a large number of classes with row duplicates. If the decorative mode is used, the number of classes will be greatly reduced and the repetition of performance can be minimized. Therefore, the decorator mode is the basic mode of the java Igamot O library. The introduction of decorators leads to an increase in flexibility and complexity. Therefore, when using the java Imax O library,
Socket, also known as sockets, applications send requests to the network through "sockets". One party suspending the phone is equivalent to shutting down the socket and revoking the connection.
A complete Socket has a locally unique Socket number. Assigned by the operating system
Socket is designed for the client / server model. Socket uses client / server mode to ingeniously solve the problem of establishing communication connections between processes.
The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.
1), server monitoring: server-side sockets do not locate specific client sockets, but are waiting for a connection to monitor the network status in real time.
2) client request: refers to the connection request made by the socket of the client, and the target of the connection is the socket of the server.
3) connection confirmation: when the server-side socket monitors or receives a connection request from the client socket. It establishes a new thread in response to the request of the client socket.
At this point, the study on "what are the problems about threads in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.