[Turn]java Multithreading example

Source: Internet
Author: User

1. Code examples

Here we make a complete example of the difference in the way the threads are generated in different ways:

 PackageCom.example.ThreadTestDemo; Public classThreadTestextendsThread { Public Static voidMain (string[] args)throwsexception{ for(inti=0;i<10;i++) {Thread T=NewMyThread ();    T.start (); } thread.sleep (10000);//let the above thread run to completionR r =NewR ();  for(inti=0;i<10;i++) {Thread T=NewThread (R);    T.start (); }  }}classMyThreadextendsthread{ Public intx = 0;  Public voidrun () {System.out.println (++x); }  }  classRImplementsrunnable{Private intx = 0;  Public voidrun () {System.out.println (++x); }  }

The output is: 1 1 1 1 1 1 1 1 1 1

1 2 5 4 3 8 10 7 6 9

The 10 threads generated by the above 10 thread objects are printed 10 times 1 when they are run. The following 10 thread objects produced by 10 threads run with 1 to 10 printed. We refer to the following 10 threads as multiple threads of the same instance (runnable instance) .

2. Several important methods of threading objects

1. Start method

After a thread object is generated, it is important to invoke its start () method if it is to produce an executing thread. When you introduce this method, you have to explain the run method at the same time. In fact, the thread object's Run method is exactly an interface callback method, which is the specific logic that you want the thread object to complete. Simply put, you do what you have to do in the Run method. And how to do, when do not need you control, you just call the Start method, the JVM will manage this thread object to generate a thread and register to the thread management system.

On the surface, the start () method calls the run () method, and in fact does not call the Run method directly. Before JDK1.5 the start () method is the local method, and how it ultimately calls the Run method is not known to the Java programmer. In JDK1.5, the original local start method is replaced by Start0 (), and another pure Java start () calls the Local method Start0 (), and a validation is done in the start () method, is to test a global variable (object variable) started, if true, start () throws an exception, does not call the local method start0 (), otherwise, it now sets the variable to True, and then calls Start0 ().

From which we can see this in order to control that a thread object can only run successfully once the start () method. This is because the thread is running to get the current environment, including security, permissions for the parent thread, priority, and so on, if a thread can run multiple times, then define a static thread to get the appropriate permissions and priorities in an environment, After running, it runs in the current environment in another environment with the same attributes as the original permissions and priorities, which can result in unpredictable results. Simply put, a thread object can only run successfully once, based on the need for thread management.

The most essential function of the start () method is to request another thread space from the CPU to execute the code in the Run method, which is two lines from the current thread and runs in a relatively separate thread space. That is, if you invoke the Run method of the thread object directly, it will, of course, execute, but it is executed in the current thread, and the following code is executed after the Run method finishes executing. When the start () method is called, the code of the Run method executes concurrently (single CPU) or parallel (multi-CPU) with the current thread.

So remember the word: The Run method that invokes the thread object does not produce a new thread, although the same execution results can be achieved, but the execution process and execution efficiency are different.

2. Sleep,join,yield method

Let's briefly talk about the functions and invocation principles of these methods.

[Turn]java Multithreading example

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.