java multithreading interview

1, multi-thread what's the use?

  (1) take advantage of multi-core CPU:

  Currently, the application server are also at least a dual-core, 4-core, 8-core or 16-core is also not uncommon, if it is single-threaded program, then on a dual-core CPU is wasted by 50%, wasted on a 4-core CPU 75%. On a single core CPU so-called "multi-threaded" That is false multithreaded , processor will only deal with the same time period of logic, but faster than switching between threads, looked like multiple threads "simultaneously" run Bale. Multi-threading on multi-core CPU is a true multi-threaded , it allows you to work at the same logic multi-segment, multi-threaded, can really play the advantages of multi-core CPU to achieve the purpose of full use of the CPU.

       (2) prevent obstruction:

  From a procedural point of view of operating efficiency, single-core CPU will not only play the advantages of multi-threaded, multi-threaded run because it will lead to thread context switches, and reduce the overall efficiency of the program on a single core CPU . But the single-core CPU we still have to multi-threaded applications, it is to prevent clogging . Just think, if a single-core CPU using a single thread, so as long as this thread is blocked, say a data remotely read it (such as access to the database to spend a lot of time), the peer has not yet been returned and no set time-out, then you the entire program before the data is returned back to stop running. Multithreading can prevent this problem, a number of threads to run simultaneously, even if a code execution thread to read data blocked, it will not affect the execution of other tasks.

2, create a thread way:

  It is generally in two ways:

  (1) Thread class inheritance

  (2) implement Runnable

  As to which is better, needless to say the latter is certainly good, because the way of implementation of the interface more flexible way than the inherited class (java can implement multiple interfaces, but can only inherit a class) , but also to reduce the degree of coupling between the program, oriented programming interface design patterns also six core principles.

Distinction 3, start () method and the run () method

  Only call the start () method, will exhibit characteristics of a multi-threaded, different threads run () method code inside alternately executed.

  If you just call the run () method, the code is executed synchronously.

4, the difference between Callable Runnable interface and interface

  The return value run Runnable interface () method is void, just do it purely to perform the run () method code in it;

  call Callable interface () method returns a value, is a generic, and Future, FutureTask can be used to obtain the results asynchronously with the execution. In fact, this is a useful feature, because the single-threaded multi-threaded harder compared to a more important reason is because the complex multi-thread full of unknown sex, whether a certain threads? Some threads execute how long? Whether certain threads when the execution of an assignment we expect the data has been completed? Not know, we can do is wait this multi-threaded task is finished it. Cancel the thread task is really useful in case Callable + Future / FutureTask but you can get the results of running multiple threads, you can not wait too long to get the data needed

5, the difference CyclicBarrier and CountDownLatch

Guess you like

Origin www.cnblogs.com/liufei1983/p/11809006.html