携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第29天,点击查看活动详情
哈喽,大家好!我是Why,一名在读学生,目前刚刚开始进入自己的编程学习生涯。虽然学习起步较晚,但我坚信做了才有0或1的可能。学了一段时间以后也是选择在掘金上分享自己的日常笔记,也希望能够在众多道友的大家庭中打成一片。 本文主要讲解线程的使用与终止,如果大家读后觉得有用的话,还请大家多多支持博主:欢迎 ❤️点赞👍、收藏⭐、留言💬 ✨✨✨个人主页:JinHuan
线程的使用方法
/*
1、怎么获取当前线程对象?
Thread t = Thread.currentThread();
返回值t就是当前线程。
2、获取线程对象的名字
String name = 线程对象.getName();
3、修改线程对象的名字
线程对象.setName("线程名字");
4、当线程没有设置名字的时候,默认的名字有什么规律?(了解一下)
Thread-0
Thread-1
Thread-2
Thread-3
.....
*/
public class ThreadTest05 {
public void doSome(){
// 这样就不行了
//this.getName();
//super.getName();
// 但是这样可以
String name = Thread.currentThread().getName();
System.out.println("------->" + name);
}
public static void main(String[] args) {
ThreadTest05 tt = new ThreadTest05();
tt.doSome();
//currentThread就是当前线程对象
// 这个代码出现在main方法当中,所以当前线程就是主线程。
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getName()); //main
// 创建线程对象
MyThread2 t = new MyThread2();
// 设置线程的名字
t.setName("t1");
// 获取线程的名字
String tName = t.getName();
System.out.println(tName); //Thread-0
MyThread2 t2 = new MyThread2();
t2.setName("t2");
System.out.println(t2.getName()); //Thread-1\
t2.start();
// 启动线程
t.start();
}
}
class MyThread2 extends Thread {
public void run(){
for(int i = 0; i < 100; i++){
// currentThread就是当前线程对象。当前线程是谁呢?
// 当t1线程执行run方法,那么这个当前线程就是t1
// 当t2线程执行run方法,那么这个当前线程就是t2
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getName() + "-->" + i);
//System.out.println(super.getName() + "-->" + i);
//System.out.println(this.getName() + "-->" + i);
}
}
}
关于线程的Sleep方法
/*
关于线程的sleep方法:
static void sleep(long millis)
1、静态方法:Thread.sleep(1000);
2、参数是毫秒
3、作用:让当前线程进入休眠,进入“阻塞状态”,放弃占有CPU时间片,让给其它线程使用。
这行代码出现在A线程中,A线程就会进入休眠。
这行代码出现在B线程中,B线程就会进入休眠。
4、Thread.sleep()方法,可以做到这种效果:
间隔特定的时间,去执行一段特定的代码,每隔多久执行一次。
*/
public class ThreadTest06 {
public static void main(String[] args) {
// 让当前线程进入休眠,睡眠5秒
// 当前线程是主线程!!!
/*try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
// 5秒之后执行这里的代码
//System.out.println("hello world!");
for(int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
// 睡眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Thread.Sleep试题
/*
关于线程的sleep方法:
static void sleep(long millis)
1、静态方法:Thread.sleep(1000);
2、参数是毫秒
3、作用:让当前线程进入休眠,进入“阻塞状态”,放弃占有CPU时间片,让给其它线程使用。
这行代码出现在A线程中,A线程就会进入休眠。
这行代码出现在B线程中,B线程就会进入休眠。
4、Thread.sleep()方法,可以做到这种效果:
间隔特定的时间,去执行一段特定的代码,每隔多久执行一次。
*/
public class ThreadTest06 {
public static void main(String[] args) {
// 让当前线程进入休眠,睡眠5秒
// 当前线程是主线程!!!
/*try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
// 5秒之后执行这里的代码
//System.out.println("hello world!");
for(int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
// 睡眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sleep的唤醒
/*
sleep睡眠太久了,如果希望半道上醒来,你应该怎么办?也就是说怎么叫醒一个正在睡眠的线程??
注意:这个不是终断线程的执行,是终止线程的睡眠。
*/
public class ThreadTest08 {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable2());
t.setName("t");
t.start();
// 希望5秒之后,t线程醒来(5秒之后主线程手里的活儿干完了。)
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 终断t线程的睡眠(这种终断睡眠的方式依靠了java的异常处理机制。)
t.interrupt(); // 干扰,一盆冷水过去!
}
}
class MyRunnable2 implements Runnable {
// 重点:run()当中的异常不能throws,只能try catch
// 因为run()方法在父类中没有抛出任何异常,子类不能比父类抛出更多的异常。
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---> begin");
try {
// 睡眠1年
Thread.sleep(1000 * 60 * 60 * 24 * 365);
} catch (InterruptedException e) {
// 打印异常信息
//e.printStackTrace();
}
//1年之后才会执行这里
System.out.println(Thread.currentThread().getName() + "---> end");
// 调用doOther
//doOther();
}
// 其它方法可以throws
/*public void doOther() throws Exception{
}*/
}
线程终止(Killed)
/*
在java中怎么强行终止一个线程的执行。
这种方式存在很大的缺点:容易丢失数据。因为这种方式是直接将线程杀死了,
线程没有保存的数据将会丢失。不建议使用。
*/
public class ThreadTest09 {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable3());
t.setName("t");
t.start();
// 模拟5秒
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 5秒之后强行终止t线程
t.stop(); // 已过时(不建议使用。)
}
}
class MyRunnable3 implements Runnable {
@Override
public void run() {
for(int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
合理地终止线程
/*
怎么合理的终止一个线程的执行。这种方式是很常用的。
*/
public class ThreadTest10 {
public static void main(String[] args) {
MyRunable4 r = new MyRunable4();
Thread t = new Thread(r);
t.setName("t");
t.start();
// 模拟5秒
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 终止线程
// 你想要什么时候终止t的执行,那么你把标记修改为false,就结束了。
r.run = false;
}
}
class MyRunable4 implements Runnable {
// 打一个布尔标记
boolean run = true;
@Override
public void run() {
for (int i = 0; i < 10; i++){
if(run){
System.out.println(Thread.currentThread().getName() + "--->" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
// return就结束了,你在结束之前还有什么没保存的。
// 在这里可以保存呀。
//save....
//终止当前线程
return;
}
}
}
}