Java 线程常用操作方法(休眠)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ikv1989/article/details/80207982

线程休眠指的是让线程执行速度稍微慢一点。休眠方法:

·public static void sleep(long time)throws InterruptedException

范例:

class MyThread implements Runnable {
	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName());
		}
	}
}

public class Demo {
	public static void main(String[] args) throws Exception {
		MyThread mt = new MyThread();
		new Thread(mt, "线程A").start();
		new Thread(mt).start();
	}
}

展开阅读全文

没有更多推荐了,返回首页