java多線程系列2-線程式控制制,java多線程2-線程

來源:互聯網
上載者:User

java多線程系列2-線程式控制制,java多線程2-線程

前面的文章已經介紹了有關線程的調度,接下來介紹如何使用方法對線程進行控制

1、線程休眠

public static void sleep(long millis)

/* * 線程休眠 *        public static void sleep(long millis) */public class ThreadSleepDemo {    public static void main(String[] args) {        ThreadSleep ts1 = new ThreadSleep();        ThreadSleep ts2 = new ThreadSleep();        ThreadSleep ts3 = new ThreadSleep();         ts1.setName("zhangsan");        ts2.setName("lisi");        ts3.setName("wangwu");         ts1.start();        ts2.start();        ts3.start();    }} public class ThreadSleep extends Thread {    @Override    public void run() {        for (int x = 0; x < 100; x++) {            System.out.println(getName() + ":" + x + ",日期:" + new Date());            // 睡眠            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}

2、線程加入

public final void join()

public class ThreadJoin extends Thread {    @Override    public void run() {        for (int x = 0; x < 100; x++) {            System.out.println(getName() + ":" + x);        }    }}/* * public final void join():等待該線程終止。  */public class ThreadJoinDemo {    public static void main(String[] args) {        ThreadJoin tj1 = new ThreadJoin();        ThreadJoin tj2 = new ThreadJoin();        ThreadJoin tj3 = new ThreadJoin();         tj1.setName("zhangsan");        tj2.setName("lisi");        tj3.setName("wangwu");         tj1.start();        try {            tj1.join();        } catch (InterruptedException e) {            e.printStackTrace();        }                tj2.start();        tj3.start();    }}

3、線程禮讓

public static void yield()

public class ThreadYield extends Thread {    @Override    public void run() {        for (int x = 0; x < 100; x++) {            System.out.println(getName() + ":" + x);            Thread.yield();        }    }}/* * public static void yield():暫停當前正在執行的線程對象,並執行其他線程。  * 讓多個線程的執行更和諧,但是不能靠它保證一個線程一次。 */public class ThreadYieldDemo {    public static void main(String[] args) {        ThreadYield ty1 = new ThreadYield();        ThreadYield ty2 = new ThreadYield();         ty1.setName("zhangsan");        ty2.setName("lisi");         ty1.start();        ty2.start();    }}

4、後台線程

public final void setDaemon(boolean on)

public class ThreadDaemon extends Thread {    @Override    public void run() {        for (int x = 0; x < 100; x++) {            System.out.println(getName() + ":" + x);        }    }} /* * public final void setDaemon(boolean on):將該線程標記為守護線程或使用者線程。 * 當正在啟動並執行線程都是守護線程時,JAVA 虛擬機器退出。 該方法必須在啟動線程前調用。  *  */public class ThreadDaemonDemo {    public static void main(String[] args) {        ThreadDaemon td1 = new ThreadDaemon();        ThreadDaemon td2 = new ThreadDaemon();         td1.setName("zhangsan");        td2.setName("lisi");         // 設定收穫線程        td1.setDaemon(true);        td2.setDaemon(true);         td1.start();        td2.start();        Thread.currentThread().setName("wuyudong");        for (int x = 0; x < 5; x++) {            System.out.println(Thread.currentThread().getName() + ":" + x);        }    }}

5、中斷線程

public final void stop()

public void interrupt()

import java.util.Date; public class ThreadStop extends Thread {    @Override    public void run() {        System.out.println("開始執行:" + new Date());         // 我要休息10秒鐘        try {            Thread.sleep(10000);        } catch (InterruptedException e) {            // e.printStackTrace();            System.out.println("線程被終止了");        }         System.out.println("結束執行:" + new Date());    }}/* * public final void stop():讓線程停止,過時了,但是還可以使用。 * public void interrupt():中斷線程。 把線程的狀態終止,並拋出一個InterruptedException。 */public class ThreadStopDemo {    public static void main(String[] args) {        ThreadStop ts = new ThreadStop();        ts.start();         // 超過三秒不醒過來,就幹掉你        try {            Thread.sleep(3000);            // ts.stop();            ts.interrupt();        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.