Multithreading in Java

Source: Internet
Author: User

Program : refers to the static files that we write that are saved in the hard disk

Process : The program code is stored in the hard disk as a static file before it is called , and is loaded into memory from the hard disk when we call it. This is the time to think that a process has been opened . a process number is registered in the operating system as long as the program is loaded (PID). process is the concept of an operating system .

Threads : When the program starts executing , the Main () method in Java begins execution , opening up in the stack memory the main () stack frame is when a thread is opened . A thread is a stack and a chain stack frame inside it. , a stack frame corresponds to a method .

Why do you have multiple threads ?

1 people needs , can be multi-threaded solution, 2 Multi-core CPU appears , Need to improve the execution efficiency of the program

How to create a thread :

1 inherit java.lang.Thread, overriding the run () method (The Run method is equivalent to the new thread Main Method , is the start and end point of program execution )

2 Create a Thread to implement the java.lang.Runnable and implement the interface of run () Method

There is only one way to start a thread :

The object name . Start (), and the run () method is called by default when it is started

Do not write the object name . Run (), This is the call to the method, not to open a new thread

Common methods for threading classes :

SetName (): give the thread a name , First name and then start , You can not change the name after the start

If you do not set a name, the default is thread-0,thread-1 ... ( It's a name at new time )

GetName (): Gets the name of the current thread

Static CurrentThread (): Gets the memory address of the current thread

Static sleep (Time): allows the current thread to sleep for a specified number of milliseconds

SetPriority (): sets the priority of the thread

Min_priority:1

Max_priority:10

Norm_priority:5

Interrupt (): forced wakeup of dormant threads

Join (): The current thread executes and then continues to execute after this thread , that is, the code for all threads after the current line must wait for the current thread to execute before executing , which is equivalent to a thread merge , becomes a single thread , or executes from top to bottom

Static yield (): in the current time slice gives way to the same priority thread , different priority does not give way , the next time slice does not necessarily let

Daemon Threads ( bottom thread )

Live , He lives first ; die , He died later . : This is the feature of the daemon thread.

Each program at run time,The default will open a daemon thread,to monitor the operation of our normal program.,when our normal program runs out of error,will jump to this daemon thread.The Daemon will do some destruction resources.,logging and other operations,and then quit .JVM.so say,Our usual program starts,by default, two threads are present

Daemon threads are provided by default by the JVM , without our control .

We can use Setdaemon () to set our thread as a daemon thread . The method must be called before the thread is started , that is in Start before calling this method

Multithreading in Java

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.