2016-7-5 Java:thread

Source: Internet
Author: User

Two different ways:

1 . Inherit Thread 2. Implement Runnable

1.

classMyThreadextendsthread{ Public Static intnum = 0; @Override Public voidrun () {//TODO auto-generated Method Stub         while(num<100) {System.out.println (Thread.CurrentThread (). GetName ()+ "+" + num++); }    }} Public classTest { Public Static voidMain (string[] args) {MyThread thread01=NewMyThread (); MyThread thread02=NewMyThread ();        Thread01.start ();    Thread02.start (); }}

2.

classYourthreadImplementsRunnable {Private Static intnum = 0; @Override Public voidrun () {//TODO auto-generated Method Stub         while(Num < 100) {System.out.println (Thread.CurrentThread (). GetName ()+ "+" + num++); }    }} Public classTest { Public Static voidMain (string[] args) {Thread thread01=NewThread (NewYourthread (), "t01"); Thread thread02=NewThread (NewYourthread (), "t02");        Thread01.start ();    Thread02.start (); }}

This synchronization problem does not occur every time.

2016-7-5 Java: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.