Java threads: Creating and starting

Source: Internet
Author: User

Java Threads: Creating and startingOne, 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. voidRun()
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 private String name;

PublicDoSomething (String name) {
This. name = name;
}

Public voidRun () {
for(inti = 0; I < 5; i++) {
for(Longk = 0; K < 100000000; k++);
SYSTEM.OUT.PRINTLN (name +": "+ i);
}
}
} publicStatic voidMain (string[] args) {
DoSomething DS1 =NewDoSomething ("Ah San");
DoSomething DS2 =NewDoSomething ("John Doe");

Thread T1 =NewThread (DS1);
Thread t2 =NewThread (DS2);

T1.start ();
T2.start ();
}
Execution Result: John Doe: 0
A three: 0
John Doe: 1
A three: 1
John Doe: 2
John Doe: 3
A three: 2
John Doe: 4
A three: 3
A three: 4

Process finished with exit code 0 2, extending the thread class implementation of the multithreaded example public testthread (String name) {
Super(name);
}

Public voidRun () {
for(inti = 0;i<5;i++) {
for(Longk= 0; K <100000000;k++);
System.out.println ( This. GetName () +" :"+i);
}
}

Public Static voidMain (string[] args) {
Thread T1 =NewTestthread ("Ah San");
Thread t2 =NewTestthread ("John Doe");
T1.start ();
T2.start ();
}
The result of the execution: C: 0
John Doe: 0
A three: 1
John Doe: 1
A three: 2
John Doe: 2
A three: 3
A three: 4
John Doe: 3
John Doe: 4

Process finished with exit code 0 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

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.