Java Create Thread

Source: Internet
Author: User

How do I create threads in Ava?

How to create a thread one: inherit the thread classStep: 1, define a class that inherits from thread, 2, overwrite the Run method in the thread class, 3, create thread of subclass object of thread class directly, 4, call Start method to open thread.second way to create a thread: Implementing the Runnable Interface1, defines the class implementation runnable interface. 2, overwrite the Run method in the interface, encapsulating the thread's task code into the Run method. 3, the thread object is created through the thread class and the subclass object of the Runnable interface is passed as a parameter to the constructor of the thread class. Why? Because the threads ' tasks are encapsulated in the Run method of the Runnable interface subclass object. Therefore, when you create a thread object, you must explicitly define the task that you want to run. 4, call the Thread object's Start method to open the thread. The benefits of implementing the Runnable interface are: 1, separating the thread's tasks from the child classes of the thread and carrying out a separate encapsulation. Encapsulates a task into an object based on object-oriented thinking. 2, avoids the limitations of Java single inheritance. Therefore, the second way to create threads is more common.Example 1:

Example 2:

Example 3:

The start () and run () differences for thread in Java:
1) Start:
Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. By invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the CPU time slice is taken, the run () method is started, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.
2) Run:
The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads.
Summary: The start  method is called to start the thread, and the Run method is just a normal method call to thread, or it executes in the main thread. both methods should be familiar, put code that needs to be processed in parallel in the run () method, and the start () method will automatically call the run () method, which is prescribed by the JVM's memory mechanism. and the run () method must be public access, and the return value type is void:
Question: I wrote two identical threads, that is, loop over and over again, then show progress.
If you run only one, about 35 seconds or so, if two runs simultaneously, it only takes about 25 seconds,
I want to know, according to the truth, when running a thread, all occupy the CPU, should be more than the execution of two faster, can be executed at the same time two times, rather than just execute one faster?AnswerThat's a good question.
If your program simply does a simple calculation, it does not involve any operations that might be suspending the thread, such as I/O reading and writing, waiting for some sort of event, and so on. On the surface, two threads, compared to a single thread, increase the overhead of switching, which should be slower.
But the problem is that you have to consider the operating system scheduling policy, usually in a thread-supporting operating system, the thread is the unit of the system scheduling, for the same process, more than one thread can be more than the CPU time, especially to increase from one to two

For example, if you have 50 threads running in your system before your program starts, then when your program starts, if he has only one thread, on average, it will get 1/51 CPU time, and if he has two threads, he will get 2/52 CPU time.
Of course, this example is a very good case, he did not consider the system in the other threads of the busy (or idle) degree, and does not consider the thread switching, but I think may be able to explain your problem.

Java Create Thread

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.