Java Programming thought reading notes (v)--multithreading

Source: Internet
Author: User

pick up the article Java Programming thought reading notes (iv)--Cloning of objects

NO1:

Daemon Thread (daemon)

Reference http://blog.csdn.net/pony_maggie/article/details/42441895

Daemon is a user thread, which can be understood as a service thread running in the background, such as a clock processing thread, an idle thread, a garbage collection thread, and so on, all daemon threads.

Daemon thread has a feature is "relatively minor", the program if all the user thread is finished, then the program itself is over, regardless of whether the daemon is over. The user thread is not, and as long as there is a user thread present, the program does not exit.

What's the use of it? For example, like a garbage collection thread, we want the user thread to exist when it exists, but when all the user threads exit, the program exits, and the garbage collection thread does not affect the exit. If the user thread is defined, the user thread does not exit as long as the garbage collection thread does not exit, and does not match the actual demand.

 Public classDaemonextendsThread {Private Static Final intSIZE = 10; Privatethread[] t =NewThread[size];  PublicDaemon () {Setdaemon (true);    Start (); } @Override Public voidrun () { for(inti = 0; i < SIZE; i++) {T[i]=NewDaemonspawn (i); }         for(inti = 0; i < SIZE; i++) {System.out.println ("t[" + i + "].isdaemon () =" +T[i].isdaemon ()); }         while(true) {yield (); }    }}
 Public class extends Thread {    public daemonspawn (int  i) {        System.out.println ("Daemonspwan" + i + "started");        Start ();    }    @Override    publicvoid  run () {        while (true  ) {            yield ();     }}}

 Public classdaemons { Public Static voidMain (String args[]) {Thread d=NewDaemon (); System.out.println ("D.isdaemon () =" +D.isdaemon ()); BufferedReader stdin=NewBufferedReader (NewInputStreamReader (system.in)); System.out.println ("Waiting for CR"); Try{stdin.readline (); } Catch(IOException e) {} }}

Print results

D.isdaemon () =trueWaiting forCrdaemonspwan0Starteddaemonspwan1Starteddaemonspwan2Starteddaemonspwan3Starteddaemonspwan4Starteddaemonspwan5Starteddaemonspwan6Starteddaemonspwan7Starteddaemonspwan8Starteddaemonspwan9startedt[0].isdaemon () =truet[1].isdaemon () =truet[2].isdaemon () =truet[3].isdaemon () =truet[4].isdaemon () =truet[5].isdaemon () =truet[6].isdaemon () =truet[7].isdaemon () =truet[8].isdaemon () =truet[9].isdaemon () =true

No2:

What is the difference between a thread group and a wire pool? (Threadgroup/threadpool)

    • Thread groups: The meaning of thread groups exists, the primary reason is security. The threads created by default in Java are all part of the system thread group, and the threads of the same thread group can modify each other's data. However, if you are in a different thread group, you cannot modify the data across thread groups to ensure data security to some extent.

    • Thread pool: The meaning of the thread pool exists, the primary function is efficiency. The creation and completion of threads takes a certain amount of system time (especially created), and it wastes a lot of time to create and delete threads. Therefore, when you create a thread and make it do not end after the task is finished, it puts it into hibernation, and then wakes up when it is needed, saving a certain amount of time. If such threads are more, then they can be managed using line pool. ensure efficiency.

    • Threads and thread Pools feature common: 1, which manages a certain number of thread 2, can control threads---including hibernation, wake-up, end, create, interrupt (pause)--but not necessarily all of them.

No3:

Causes of thread blockage:

(1) Call sleep (in milliseconds) to put the thread into a "sleep" state. This thread will not run for a specified period of time

(2) The execution of the thread is paused with suspend (). The "operational" state is not returned unless the thread receives a resume () message (both methods are not recommended because it is unsafe)

(3) Suspend execution of the thread with wait (), unless the thread receives a notify () or notifyall () message, it does not become "operational"

(4) thread is waiting for some IO (input and output) operation to complete

(5) The thread tries to invoke the "synchronous" method of another object, but that object is locked and temporarily unavailable

Java Programming thought reading notes (v)--multithreading

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.