Java thread and runnable differences

Source: Internet
Author: User

Package Com.tonyluis;class MyThread extends thread{private int ticketnum;      private String name;         Public MyThread (String name,int ticketnum) {super ("Thread:" +name);//thread name THIS.name =name;    This.ticketnum=ticketnum;    } public void Run () {int tmp=this.ticketnum;        String threadname = Thread.CurrentThread (). GetName (); for (int i =0;i<tmp;i++) {if (this.ticketnum>0) {System.out.println ("ThreadName:" +thread              Name+ "" +this.name+ "remaining" + (this.ticketnum--));    }}}}class MyThread1 implements runnable{private int ticketnum = 10;    public void Run () {int tmp=this.ticketnum;        String threadname = Thread.CurrentThread (). GetName (); for (int i =0;i<tmp;i++) {if (this.ticketnum>0) {System.out.println ("ThreadName:" +thread              name+ "" + "remaining" + (this.ticketnum--)); }}}} public class Testthread {public static void main (String[] args) {New MyThread ("Window 1", 3). Start ();        New MyThread ("Windows 2", 4). Start ();        New MyThread ("Windows 3", 2). Start ();         MyThread1 MT = new MyThread1 ();        New Thread (MT, "Window 4"). Start ();        New Thread (MT, "Window 5"). Start ();            New Thread (MT, "Window 6"). Start ();   }    }

Operation Result:

Threadname:thread: Window 3 window 3 remaining 2
Threadname:thread: Window 1 window 1 remaining 3
Threadname:thread: Window 2 window 2 remaining 4
Threadname:thread: Window 1 window 1 remaining 2
Threadname:thread: Window 3 window 3 remaining 1
Threadname:thread: Window 1 window 1 remaining 1
Threadname:thread: Window 2 window 2 remaining 3
Threadname:thread: Window 2 window 2 remaining 2
Threadname:thread: Window 2 window 2 remaining 1
ThreadName: Window 5 remaining 10
ThreadName: Window 4 remaining 9
ThreadName: Window 6 remaining 10
ThreadName: Window 4 remaining 7
ThreadName: Window 5 remaining 8
ThreadName: Window 4 remaining 5
ThreadName: Window 6 remaining 6
ThreadName: Window 4 remaining 3
ThreadName: Window 5 remaining 4
ThreadName: Window 4 remaining 1
ThreadName: Window 6 remaining 2

Inheriting thread is generally the task of multiple threads to complete their own tasks, implementation of runable can implement multiple threads together to complete a task , and the thread needs to be implemented through the internal class, the implementation is more troublesome

See: http://blog.csdn.net/yilip/article/details/8147027

interfaces cannot have constructors, so when you use interfaces, you cannot pass variables by using constructors .

Java thread and runnable differences

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