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();
        }
    }
}

 

posted @ 2016-05-14 10:06  wuyudong  阅读(473)  评论(0编辑  收藏  举报
Top_arrow