多线程常用方法(一)

来源:互联网 发布:php相似图片搜索 api 编辑:程序博客网 时间:2024/06/06 00:21

这里写图片描述

线程控制有关方法
start() 新建的线程进入Runnable状态
run() 线程进入Running 状态
wait() 线程进入等待状态,等待被notify,这是一个对象方法,而不是线程方法
notify()/notifyAll() 唤醒其他的线程,这是一个对象方法,而不是线程方法
yield() 线程放弃执行,使其他优先级不低于此线程的线程有机会运行,它是一个静态方法
getPriority()/setPriority() 获得/设置线程优先级
suspend() 挂起该线程,Deprecated,不推荐使用
resume() 唤醒该线程,与suspend相对,Deprecated,不推荐使用
sleep() 线程睡眠指定的一段时间
join() 调用这个方法的主线程,会等待加入的子线程完成

线程名称
1,在Thread类中可以通过getName()方法取得线程名称,通过setName()设置线程名称。
2,线程的名称一般在启动线程前设置,但也允许为运行的线程设置名称,允许两个Thread对象有相同名称,但是应该避免。
3,如果程序没有为线程指定名称,系统会自动为线程设置名称。

class MyThread implements Runnable{    // 实现Runnable接口    public void run(){    // 覆写run()方法        for(int i=0;i<3;i++){            System.out.println(Thread.currentThread().getName()                    + "运行,i = " + i) ;    // 取得当前线程的名字        }    }};public class ThreadNameDemo{    public static void main(String args[]){        MyThread mt = new MyThread() ;    // 实例化Runnable子类对象        new Thread(mt).start() ;        // 系统自动设置线程名称        new Thread(mt,"线程-A").start() ;        // 手工设置线程名称        new Thread(mt,"线程-B").start() ;        // 手工设置线程名称        new Thread(mt).start() ;        // 系统自动设置线程名称        new Thread(mt).start() ;        // 系统自动设置线程名称    }};

当前线程:CurrentThread()
程序可以通过currentThread()方法取得当前正在运行的线程对象,

class MyThread implements Runnable{    // 实现Runnable接口    public void run(){    // 覆写run()方法        for(int i=0;i<3;i++){            System.out.println(Thread.currentThread().getName()                    + "运行,i = " + i) ;    // 取得当前线程的名字        }    }};public class CurrentThreadDemo{    public static void main(String args[]){        MyThread mt = new MyThread() ;    // 实例化Runnable子类对象        new Thread(mt,"线程").start() ;        // 启动线程        mt.run() ;    // 直接调用run()方法    }};

判断线程是否在执行:isAlive

class MyThread implements Runnable{    // 实现Runnable接口    public void run(){    // 覆写run()方法        for(int i=0;i<3;i++){            System.out.println(Thread.currentThread().getName()                    + "运行,i = " + i) ;    // 取得当前线程的名字        }    }};public class ThreadAliveDemo{    public static void main(String args[]){        MyThread mt = new MyThread() ;    // 实例化Runnable子类对象        Thread t = new Thread(mt,"线程");        // 实例化Thread对象        System.out.println("线程开始执行之前 --> " + t.isAlive()) ;     // 判断是否启动        t.start() ;    // 启动线程        System.out.println("线程开始执行之后 --> " + t.isAlive()) ;     // 判断是否启动        for(int i=0;i<3;i++){            System.out.println(" main运行 --> " + i) ;        }        // 以下的输出结果不确定        System.out.println("代码执行之后 --> " + t.isAlive()) ;     // 判断是否启动    }};

线程强制运行:join()
可以通过join()方法使得一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后,才可以继续运行。

class MyThread implements Runnable{    // 实现Runnable接口    public void run(){    // 覆写run()方法        for(int i=0;i<50;i++){            System.out.println(Thread.currentThread().getName()                    + "运行,i = " + i) ;    // 取得当前线程的名字        }    }};public class demo1{    public static void main(String args[]){        MyThread mt = new MyThread() ;    // 实例化Runnable子类对象        Thread t = new Thread(mt,"线程");        // 实例化Thread对象        t.start() ;    // 启动线程        for(int i=0;i<50;i++){            if(i>10){                try{                    t.join() ;    // 线程强制运行                }catch(InterruptedException e){}            }            System.out.println("Main线程运行 --> " + i) ;        }    }};

参考博客:http://www.cnblogs.com/alsf/p/5651888.html

原创粉丝点击