Java threads: Creating and starting

Source: Internet
Author: User

Java Threads: Creating and startingSCJP5 Study Notes One, define the thread1, extended Java.lang.Thread class. There is a run () method in this class, and you should be aware of its usage:
Run ()
If the thread is constructed using a stand-alone Runnable running object, the method of the object is called, Runnable run Otherwise the method does nothing and returns.  
Thread Subclasses should override this method.
2, realize java.lang.Runnable interface. void Run()
Runnable When you create a thread with an object that implements an interface, starting the thread causes the method of the object to be called in a thread that executes independently run .
run The general contract for a method is that it may perform any required operations.
second, instantiate the thread1, if the extension of the Java.lang.Thread class thread, then the direct new can. 2, if the implementation of the Java.lang.Runnable interface class, then use the thread construction method: Thread (Runnable target)
Thread (Runnable target, String name)
Thread (threadgroup group, Runnable target)
Thread (threadgroup group, Runnable Target, String name)
Thread (threadgroup group, Runnable Target, String name, long stackSize) third, start the threadCall the Start () method on the thread object of the thread, not run () or any other method. Before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread. After the start () method is called: a complex set of things starts a new execution thread (with a new call stack), the thread is moved from new state to the operational state, and the target run () method runs when the thread gets an opportunity to execute. Note: For Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread. Iv. Examples1. Multithreading example of implementing Runnable interface
Package Com.dashu.thread;public class DoSomething implements Runnable {private string name;public dosomething (String Name) { = name;} public void Run () {//TODO auto-generated method stubfor (int i = 0; i <, i++) {for (long k = 0; k < 100000000; k ++); SYSTEM.OUT.PRINTLN (name + ":" + i);}}}


Package Com.dashu.thread;public class Testthread {public static void main (string[] args) {dosomething do1 = new Dosomethin G ("Tortoise");D osomething do2 = new DoSomething ("Wang Ba"); thread T1 = new Thread (DO1); Thread t2 = new Thread (DO2); T1.start (); T2.start ();}}


2. Multithreading example of extending the thread class implementation

Package Com.dashu.thread;public class Testthread extends thread{public     testthread (String name) {         super (name);     }     public void Run () {for         (int i = 0;i<5;i++) {for             (long k= 0; k <100000000;k++);             System.out.println (This.getname () + ":" +i);         }     }     public static void Main (string[] args) {         Thread t1 = new Testthread ("King VIII");         Thread t2 = new Testthread ("turtle");         T1.start ();         T2.start ();     } }

The results and methods of implementation are similar.

For the above multithreaded program code, the result of the output is indeterminate. One of the statements for (long k= 0; k <100000000;k++) is used to simulate a very time-consuming operation. v. Some FAQs1, the name of the thread, a running thread always has a name, the name has two sources, one is the name of the virtual machine itself, one is your own fixed name. In the absence of a specified thread name, the virtual machine always assigns a name to the thread, and the main thread's name is always Mian, and the name of the non-main thread is indeterminate. 2, the thread can be set name, can also get the name of the thread, even the main course is no exception. 3. The method to get the object of the current thread is: Thread.CurrentThread (); 4, in the above code, only guarantee: Each thread will start, each thread will run until completion. A series of threads starting in some order does not mean that they will be executed in that order. For any set of startup threads, the scheduler does not guarantee its execution order and the duration is not guaranteed. 5. When the thread target run () method ends, the thread finishes. 6. Once the thread is started, it can never be restarted again. Only one new thread can be started, and only once. A running thread or a dead thread can be restarted. 7, thread scheduling is a part of the JVM, on a CPU machine, in fact, can only run one thread at a time. Only one thread stack executes at a time. The JVM thread scheduler determines which threads are actually running in a running state. One of the many running threads can be selected as the current thread. The order in which the running threads are selected to run is not guaranteed. 8, although the queue form is usually used, but this is not guaranteed. The queue form is when a thread completes a "round", it moves to the tail of the running queue until it finally queues up to the front end of the queue before it can be selected again. In fact, we call it a running pool instead of a running queue, to help realize that threads are not all in a guaranteed order to sing a queue. 9, although we do not have the ability to control the thread scheduler, but can be other ways to affect the way thread scheduling.

Java threads: Creating and starting

Related Article

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: 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.