package com;
/**
* 关于sleep();
* static void sleep (long millis)
* 1、静态方法:Thread.sleep(1000);
* 2、参数是毫秒
* 3、作用是:让当前的线程进入休眠,进入”阻塞状态“,放弃占用cpu时间片,让其线程使用。
* 这行代码出现A线程中,A线程就会进入休眠
* 这行代码出现B线程中,B线程就会进入休眠。
* 4、Thread.sleep方法的效果:
* 间隔特定时间,去执行一段特定的代码,每隔多久去执行一次。
*/
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + i);
//睡眠1s
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println("HelloWorld");//5s后执行
}
}
}
终止sleep()方法
package com;
/**
* 怎么终止正在睡眠的的线程?
* 注意这个不是终端线程的执行。是终止线程的睡眠。
*/
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyThread());
t.setName("t");
t.start();
//希望5s后, t线程醒来。。
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//中断t线程的睡眠(这种中断睡眠的方式s是依靠了java的异常机制)
t.interrupt();//干扰
}
}
class MyThread implements Runnable {
//重点:run方法中的异常不能throws,只能try..catch
//因为run()方法中的父类中没有抛出任何异常,子类不能比父类,抛出更多的异常。
public void run() {
System.out.println(Thread.currentThread().getName() + "----->" + "begin");
//睡眠一年
try {
Thread.sleep(1000 * 60 * 60 * 24 * 365);
} catch (InterruptedException e) {
//打印异常信息
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "----->" + "end");
}
}
结果
t----->begin
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.MyThread.run(Test.java:33)
at java.lang.Thread.run(Thread.java:745)
t----->end
强行终止一个线程执行
package com;
/**
* 在java中怎么强行终止一个线程执行?
* 这种方式存在很大缺点,容易丢失数据,因为这种直接将线程杀死。
* 线程没有保存数据,会丢失。
*
*/
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyThread());
t.setName("t");
t.start();
//希望5s后, t线程醒来。。
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//5s后强行终止t线程
t.stop();//已经过时,不建议使用。
}
}
class MyThread implements Runnable {
public void run() {
for (int i = 0; 1 <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+"begin");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
合理的终止一个线程的执行
package com;
public class Test {
public static void main(String[] args) {
MyThread r = new MyThread();
Thread t1 = new Thread(r);
t1.setName("t");
t1.start();
//希望5s后, t线程醒来。。
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//终止线程
//你想要什么时候结束,把标记为false,就结束。
r.run = false;
}
}
class MyThread implements Runnable {
//打一个布尔标记
boolean run = true;
public void run() {
for (int i = 0; 1 < 10; i++) {
if (run) {
System.out.println(Thread.currentThread().getName() + "--->" + "begin");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
//在这里保存
//终止当前程序
return;
}
}
}
}
sleep面试题
package com;
public class Test {
public static void main(String[] args) {
//创建线程对象
MyThread myThread=new MyThread();
myThread.setName("t");
myThread.start();
//调用sleep方法
/**
* 这段代码会不会让t休眠?
* 不会
*/
try {
myThread.sleep(1000*5);//这段代码让main线程休眠
} catch (InterruptedException e) {
e.printStackTrace();
}//5s后才会执行
System.out.println("HelloWorld");
}}
class MyThread extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
}
}
线程合并
join();
package com;
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new Thread1());
t.setName("t");
t.start();
//合并线程
try {
t.join();//t合并到当前线程,当前线程受阻塞,t线程执行直到结束
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main over");
}
}
class Thread1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
}
版权声明:本文为weixin_46143360原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。