多线程(二)——线程的基本操作

线程的状态转换
http://blog.csdn.net/pcceo1/article/details/52444730
1.停止线程
Thread.stop(),已经被弃用的方法,不推荐使用。

public class ThreadStop implements Runnable{
    @Override
    public void run() {
        try {
            System.out.println("开始");
            Thread.sleep(10000);
            System.out.println("线程睡了10秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadStop());
        //启动线程
        thread.start();
        //停止线程
        thread.stop();
    }

}

这个程序执行的时候在控制台并没有输出什么,说明线程在启动后,还没有开始执行方法体,线程已经被停止了。
尝试在主线程中睡眠2秒钟,再停止线程。

package cn.zengzehao.thread;

public class ThreadStop implements Runnable{
    @Override
    public void run() {
        try {
            System.out.println("开始");
            Thread.sleep(10000);
            System.out.println("线程睡了10秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadStop());
        thread.start();
        try {
        //休眠2秒
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        thread.stop();
    }

}

程序执行后,在控制台输出了“开始”,2秒钟后结束线程。

通过上面的例子,使用Thread.stop()方法停止线程,不管线程的方法体有没有执行完,都会被停止掉,这样子会容易导致数据不一致的问题,比如在一个线程中写入数据,写到一半线程就被停止掉了。

2.线程的中断
线程被中断,并不是真正去中断线程,而是给线程设置了一个标志位,在线程执行的时候可以检查线程是否被设置了中断。

public void interrupt() // 中断线程
public boolean isInterrupted() // 判断是否被中断
public static boolean interrupted() // 判断是否被中断,并清除当前中断状态

直接看程序吧

package cn.zengzehao.thread;

public class ThreadInterrupted implements Runnable{
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadInterrupted());
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        thread.interrupt();
        //thread.stop();
    }
    @Override
    public void run() {
        for(;;){
            System.out.println("线程开始执行");
            System.out.println("line22"+Thread.currentThread().isInterrupted());
            if(Thread.currentThread().isInterrupted()){
                System.out.println("线程已经被中断");
                break;
            }
            long sum = 0;
            for(long i=0;i<100000;i++){
                sum+=i;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                System.out.println("line37"+Thread.currentThread().isInterrupted());
                //设置中断状态,抛出异常后会清除中断标记位
                Thread.currentThread().interrupt();
                System.out.println("line39"+Thread.currentThread().isInterrupted());
            }
            System.out.println(sum);
        }
    }

}

执行程序,如果调用interrupted(),线程会把方法体剩下的执行完,如果是调用stop(),线程会直接就结束。stop()方法太暴力了,不推荐使用。

3.线程的挂起和继续执行线程
– suspend()不会释放锁
– 如果加锁发生在resume()之前 ,则死锁发生

4.等待线程的结束 jion()
在多线程中,经常需要等待其他线程执行完,再进行下面的操作,这个时候就需要用到jion()

package cn.zengzehao.thread;

public class ThreadJion implements Runnable{
    public volatile static int i =0;
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadJion());
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main方法"+i);
    }

    @Override
    public void run() {
        for(i=0;i<1000000;i++);
        System.out.println("方法内:"+i);
    }
}

如果不使用jion(),运行结果是

main方法0
方法内:1000000

使用jion(),运行结果是

main方法1000000
方法内:1000000

5.线程的谦让 yield()
一个调用yield()方法的线程告诉虚拟机它乐意让其他线程占用自己的位置。

6.线程等待wait()
7.线程的唤醒notify()和notifyAll()

package cn.zengzehao.thread;

public class ThreadWaitAndNotify {
    final static Object object= new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(new T1());
        Thread t2 = new Thread(new T2());
        t1.start();
        t2.start();
    }
    static class T1 implements Runnable{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T1线程开始");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("T1线程结束");
            }

        }

    }
    static class T2 implements Runnable{

        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T2线程开始");
                object.notify();
                System.out.println("T2线程结束");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

notify()会随机唤醒一个在等待的线程。
notifyAll()会唤醒全部的线程。

package cn.zengzehao.thread;

public class ThreadWaitAndNotify {
    final static Object object= new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(new T1());
        Thread t2 = new Thread(new T2());
        Thread t3 = new Thread(new T3());
        Thread t4 = new Thread(new T4());
        t1.start();
        t3.start();
        t4.start();
        //休眠100毫秒,为了唤醒线程可以在其他线程都已经启动
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start();
    }
    static class T1 implements Runnable{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T1线程开始");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("T1线程结束");
            }

        }

    }
    static class T3 implements Runnable{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T3线程开始");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("T3线程结束");
            }

        }

    }

    static class T4 implements Runnable{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T4线程开始");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("T4线程结束");
            }

        }

    }
    static class T2 implements Runnable{

        @Override
        public void run() {
            synchronized (object) {
                System.out.println("T2线程开始");
                object.notify();
                System.out.println("T2线程结束");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

你可能感兴趣的:(多线程)