Java學習筆記-Thread-線程

來源:互聯網
上載者:User

標籤:

//2015年5月5日16:55:00

//Main

package com.alfredsun.thread;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubMyThread t1=new MyThread("A");MyThread t2=new MyThread("B");//t1.run();//t2.run();//線程的啟動由start()t1.start();t2.start();}}

  //MyThread

package com.alfredsun.thread;public class MyThread extends Thread{    private String name;    public MyThread(String name)    {        this.name =name;    }    public void run()    {        for(int i=0;i<10;i++)        {            System.out.println(name+":"+i);        }        super.run();    }}

//並髮結果

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

 

 

//Runnable介面

//2015年5月5日17:04:03

//mainpackage com.alfredsun.thread;public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        MyRunable r1= new MyRunable("A");        MyRunable r2=new MyRunable("B");        Thread t1=new Thread(r1);        Thread t2=new Thread(r2);        t1.start();        t2.start();    }}
//Myrunnablepackage com.alfredsun.thread;public class MyRunable implements Runnable {    private String name;    public MyRunable(String name)    {        this.name=name;    }    public void run()    {        for(int i=0;i<1000;i++)        {            System.out.println(name+":"+i);        }    }}

 //線程的操作

//2015年5月5日17:31:47

 1 package com.alfredsun.thread; 2 class RunnableDemo implements Runnable 3 { 4     private String name; 5     public RunnableDemo(String name) { 6     this.name =name; 7     } 8     public void run() 9     {10         for(int i=0;i<50;i++)11         {12             System.out.println(Thread.currentThread().getName());13         }14     }15 }16 public class test {17         public static void main(String[] args)18         {19             RunnableDemo r1= new RunnableDemo("A");20             RunnableDemo r2= new RunnableDemo("B");21             Thread t1 =new Thread(r1);22             Thread t2 =new Thread(r2);23             24             t1.start();25             t2.start();26         }27 }

 

Java學習筆記-Thread-線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.