【JAVA之多线程】2.运行状态+基本方法

来源:互联网 发布:免费翻墙安卓软件 编辑:程序博客网 时间:2024/06/06 05:01

一、运行状态

这里写图片描述

  • 多个线程时,CPU某一时刻只执行一个线程,其他线程等待被执行,处于临时(阻塞)状态。
  • 某个线程正在运行,突然不想再继续运行,想休息一会,采用sleep/wait,进入冻结状态,等待休息时间到了,或者被唤醒notify,拥有了运行资格,进入阻塞状态,等待被执行。
  • 运行时,使用stop,强行停止线程,结束运行(消亡)。或者run方法内的执行代码执行完毕,该线程也会消亡。

二、基本方法

这里写图片描述

  • sleep方法
public class Demo {    public static void main(String[] args) {        T d = new T();        d.start();        try {            Thread.sleep(10000);//该方法在哪个线程里就是sleep哪个线程,主线程睡眠10s        } catch(InterruptedException e) {}        d.interrupt();//中断线程,比较粗暴        //d.stop();比interrupt()还要粗暴,最好不要用,直接让线程死掉        //flag = false;使用这种方法让线程停掉    }}class T extends Thread {    //boolean flag = true;    public void run() {        while(true) {//while(flag)            sopl("-----" + new Date() + "-----");            try {                sleep(1000);//每隔1s更新一次时间            } catch(InterruptedException e) {                return;//如果线程被中断,直接结束            }        }    }}
  • 线程名称
//线程拥有自己默认的名称//Thread.getName();Thread-编号(从0开始)public class Demo {    public static void main(String[] args){        T d = new T();        d.start();        for(int i = 0; i < 5; i++) {            System.out.println("Hello World----" + i);        }    }}class T extends Thread {    public void run() {        for(int i = 0; i < 5; i++) {            System.out.println(this.getName() + i);        }    }}//运行结果:Hello World----0Thread-00Hello World----1Hello World----2Hello World----3Hello World----4Thread-01Thread-02Thread-03Thread-04//自定义线程名称:setName/构造方法public class Text {    public static void main(String[] args){        Demo d = new Demo("1: ");        d.start();        for(int i = 0; i < 5; i++) {            System.out.println("Hello World----" + i);        }    }}class Demo extends Thread {    public Demo(String name) {        super(name);    }    public void run() {        for(int i = 0; i < 5; i++) {            System.out.println(this.getName() + i);            //等价于System.out.println(Thread.currentThread().getName() + i);            //Thread.currentThread()返回当前线程对象==this,标准通用方式        }    }}//运行结果:Hello World----01: 0Hello World----11: 1Hello World----21: 2Hello World----31: 3Hello World----41: 4
  • join:临时加入线程执行,直到该线程执行结束
class JoinDemo extends Thread {    public JoinDemo(String name) {        super(name);    }    public void run() {        for(int x = 0; x < 10; x++) {            System.out.println(getName() + "..." + x);        }    }}public class Demo {    public static void main(String[] args) throws InterruptedException{        JoinDemo b1 = new JoinDemo("T1");//线程名称为T1        JoinDemo b2 = new JoinDemo("T2");//线程名称为T1        b1.start();        b2.start();//T2不受T1影响        try {            b1.join();//T2和T1正常执行,主线程放弃执行权,直到T1执行结束,主线程才开始继续执行(与T2无关)        } catch (InterruptedException w) {}        //b2.start();//T1执行结束,T2和主线程正常执行        for(int x = 0; x < 10; x++) {            System.out.println("main throw");        }    }}//运行结果:T1...0T1...1T2...0T1...2T2...1T1...3T2...2T1...4T2...3T1...5T1...6T1...7T1...8T1...9T2...4T2...5main throwmain throwmain throwmain throwmain throwmain throwmain throwmain throwmain throwmain throwT2...6T2...7T2...8T2...9
  • yield:让出CPU,让其他线程执行
class JionDemo implements Runnable {    public void run() {        for(int x = 1; x <= 30; x++) {            sopl(Thread.currentThread().getName() + ":" + x);            if((x % 10) == 0) {                Thread.yield();                //强制让正在运行的线程释放执行权,临时释放,稍微能减缓线程的运行,使线程有几乎平均运行的效果            }                          }    }}public class Demo {    public static void main(String[] args) throws InterruptedException{        JionDemo b = new JionDemo();//同一个线程类对象可以启两个线程        Thread t1 = new Thread(b);          Thread t2 = new Thread(b);        t1.start();        t2.start();    }}
  • setPriority(int newPriority):更改线程的优先级(共10级:1-10)
    • 设置不设置不明显,依旧互相抢夺执行权,只是抢的厉害或弱一些

这里写图片描述

0 0
原创粉丝点击