JAVA creation thread, java thread

Source: Internet
Author: User

JAVA creation thread, java thread

To enable multi-threaded objects, you only need to inherit the java. lang. Thread class or implement the java. lang. Runnable interface. Java threads are implemented through the java. lang. Thread class. After a Thread class object is generated, a new Thread is generated. This instance object can be used to start, terminate, and suspend threads. Its construction method is as follows:
Public Thread (ThreadGroup group, Runnable target, String name );
The first parameter is the thread group to which the thread belongs; the target is the target object of the actual execution thread body, which must implement interface Runnable; name is the thread name

In addition, when a parameter is null, there are the following methods:
Public Thread ();
Public Thread (Runnable target );
Public Thread (Runnable target, String name );
Public Thread (String name );
Public Thread (ThreadGroup group, Runnable target );
Public Thread (ThreadGroup group, String name );



Any object that implements the interface Runnable can be used as the target object of a thread. The Thread class also implements the interface Runnable. Therefore, you can implement the Thread body in two ways:
(1) define a Thread class, inherit the Thread class, and override the run () method. At this time, when initializing the instance of this class, the target can be null, indicating that the instance object is executed.

Class ComputerSum {int sum; public void setSum (int n) {sum = n;} public int getSum () {return sum ;}} class People extends Thread {int timeLength; computerSum CS; People (String s, int timeLength, ComputerSum sum) {setName (s); // call setName () this of the Thread class. timeLength = timeLength; CS = sum;} public void run () {for (int I = 1; I <= 5; I ++) {int m = CS. getSum (); CS. setSum (m + 1); System. out. println ("I am" + getName () + ", and:" + CS. getSum ();} try {sleep (timeLength);} catch (InterruptedException e) {}} public class ChildThread {public static void main (String args []) {People teacher, student; ComputerSum sum = new ComputerSum (); teacher = new People ("instructor", 200, sum); student = new People ("student", 200, sum); teacher. start (); student. start ();}}

 


(2) provide a class that implements interface Runnable as the target object of a Thread. When initializing the Thread object of a Thread class or Thread subclass, pass the target to this Thread instance, run () is provided by the target object ().

Class A implements Runnable {Thread th; A () {th = new Thread (this, "Demo Thread");} public void run () {System. out. println ("class A implements the Runnable interface and can inherit other classes");} public class RunnableThread {public static void main (String args []) {A a = new A ();. th. start (); System. out. println ("this is the main 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.