Java Learning Note-thread-thread

Source: Internet
Author: User

May 5, 2015 16:55:00

Main

Package Com.alfredsun.thread;public class Main {public static void main (string[] args) {//TODO auto-generated method stub MyThread t1=new MyThread ("A"); MyThread t2=new MyThread ("B");//t1.run ();//t2.run ();//Thread startup by Start () T1.start (); T2.start ();}}

MyThread

 PackageCom.alfredsun.thread; Public classMyThreadextendsthread{PrivateString name;  PublicMyThread (String name) { This. Name =name; }     Public voidrun () { for(inti=0;i<10;i++) {System.out.println (name+":"+i); }        Super. Run (); }}

Concurrency results

a:0B:0A:1B:1A:2B:2A:3B:3A:4B: 4A:5B:5B:6A:6B:7A:7B:8B: 9A:8A:9

Runnable interface

May 5, 2015 17:04:03

//Main PackageCom.alfredsun.thread; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubMyrunable r1=NewMyrunable ("A"); myrunable R2=NewMyrunable ("B"); Thread T1=NewThread (R1); Thread T2=NewThread (R2);        T1.start ();    T2.start (); }}
//myrunnable PackageCom.alfredsun.thread; Public classMyrunableImplementsRunnable {PrivateString name;  Publicmyrunable (String name) { This. name=name; }     Public voidrun () { for(inti=0;i<1000;i++) {System.out.println (name+":"+i); }    }}

Operation of the thread

May 5, 2015 17:31:47

1  PackageCom.alfredsun.thread;2 classRunnabledemoImplementsRunnable3 {4     PrivateString name;5      PublicRunnabledemo (String name) {6      This. Name =name;7     }8      Public voidRun ()9     {Ten          for(inti=0;i<50;i++) One         { A System.out.println (Thread.CurrentThread (). GetName ()); -         } -     } the } -  Public classTest { -          Public Static voidMain (string[] args) -         { +Runnabledemo r1=NewRunnabledemo ("A"); -Runnabledemo r2=NewRunnabledemo ("B"); +Thread T1 =NewThread (R1); AThread t2 =NewThread (R2); at              - T1.start (); - T2.start (); -         } -}

Java Learning Note-thread-thread

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.