Java thread and java multi-thread instance

Source: Internet
Author: User

Java thread and java multi-thread instance

   
 
  • Thread concept:

Almost every operating system supportsProcessProcess is a program that runs independently to some extent.

   

Process--- CPU time used for polling between programs. A process is a CPU task.

   

Thread--- In the program, polling the execution time obtained by the program. A thread is a task of a program.

   

Multi-Thread)Extended the concept of Multi-Process operations, and reduced the division of tasks to the program level, makingVarious ProgramsIt seems that you can execute multiple tasks at the same time.

   

Each task is called a thread,A program that can run multiple threads simultaneously is called a multi-thread program..

   

Multi-process and multi-thread have been two methods of resource scheduling for a long time. However, Java is the first mainstream programming language, instead of simply scheduling the underlying operating system.

In fact, each Java application has at least one thread-the main thread. When a Java application is East, JVM creates a main thread and calls the main () method of the program in this thread.

   

What is the difference between multi-process and multi-thread? For a process, each process has its own complete set of variables, while the thread shares the same data.

   

We know that three elements of computer program execution are CPU, program code, and accessible data. In Java, multithreading is implemented through virtual CPU.

It can be visually understood that multiple computers are virtualized in a Java program. Each computer corresponds to a thread and has its own CPU to obtain the required code and data, therefore, tasks can be executed independently,

Code and data can also be shared between them. Java threads are implemented through the java. lang. Thread class. It implements the virtual CPU function internally, can receive and process the code and data passed to it, and provides

Independent operation control function.

   

The JVM usually creates some other threads. However, these threads are usually invisible to us. For example, threads used for automatic garbage collection, object termination, or other JVM processing tasks.

 

  • Thread creation method:
   

Method 1 (inheriting the Thread class ):

Inherit a class from the Thread and overwrite the run () method in the Thread so that the class itself becomes a Thread class:

Public class Aclass extends Thread {... Public void run (){.... }...} Aclass a = new Aclass ();. start (); // use the start () method, and the thread enters the Runnable (Runnable State). It will register this thread with the thread scheduler. Will not enter the Running status immediately (Running)
  

Method 2 (implementing the Runnable interface ):

public Bclass implements Runnable{        public void run(){              …        }}Bclass b =new Bclass();b.start();

Additional knowledge:

Public class Thread {.......... Public Thread (Runnable target) {Init (null, target, "Thread-" + nextThreadNum (), 0);} privateRunnable target; Public voidRun() {If (target! = Null ){Target.Run() ;}}/*** Causes this thread to begin execution; the Java Virtual Machine * CILS the <code> run </code> method of this thread. * run this thread. JVM calls the run method of this thread. * <P> * The result is that two threads are running concurrently: the * current thread (which returns from the call to the * <code> start </code> method) and the other thread (which executes its * <code> run </code> method ). * The result is that two threads are run: The current thread (by calling the start method) and the other thread (executing its run method) * <p> * It is never legal to start a thread more than once. * The start method of a thread is never called twice. * In particle, a thread may not be restarted once it has completed * execution. * generally, a thread does not have to be executed again after it is called and executed once. * @ Exception IllegalThreadStateException if the thread was already * started. * @ see # run () * @ see # stop () */public synchronized voidStart() {/*** This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. any new functionality added * to this method in the future may have to also be added to the VM. ** A zero status value corresponds to state "NEW ". */if (threadStatus! = 0 | this! = Me) throw new IllegalThreadStateException (); group. add (this );Start0(); If (stopBeforeStart) {stop0 (throwableFromStop) ;}} privateNativeVoidStart0();..........}

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.