线程使用:Thread的基本用法
一、线程的创建
方法一:继承Thread
//继承Thread来创建一个线程类
class MyThread extends Thread{
@Override
public void run(){
System.out.println("hello Thread");
}
}
public class Dome1 {
public static void main(String[] args) {
Thread t=new MyThread();
//调用start方法启动线程
t.start();
}
}
方法二: 实现 Runnable 接口
//实现Runnable接口。
class MyRunnable implements Runnable{
@Override
public void run(){
System.out.println("hello Thread");
}
}
public class Dome3 {
public static void main(String[] args) {
Thread t=new Thread(new MyThread());
t.start();
}
}
方法三:匿名内部类创建 Thread 子类对象
public class Dome4 {
//使用匿名内部类创建Thread子类对象
public static void main(String[] args) {
Thread t=new Thread(){
@Override
public void run(){
System.out.println("hello Thread");
}
};
t.start();
}
}
方式四:匿名内部类创建 Runnable 子类对象
public class Dome5 {
//使用匿名内部类创建子类对象
public static void main(String[] args) {
Thread t=new Thread(new Runnable(){
@Override
public void run(){
System.out.println("hello Thread");
}
});
t.start();
}
}
方法五:
public class Dome6 {
//lambda 表达式创建 Runnable 子类对象
public static void main(String[] args) {
Thread t=new Thread(()->{
System.out.println("hello Thread");
});
t.start();
}
}
二:线程中断
完成一个任务往往需要坚持到最后,但是有时任务出错,(或者这个任务完成后会造成更大的损失时)就要停下来,不去执行了。
方法一:手动设置一个标记位
public class Dome1 {
private static class MyRunnable implements Runnable {
public volatile boolean isQuit = false;
@Override
public void run() {
while (!isQuit) {
System.out.println(Thread.currentThread().getName()
+ ": 别管我,我忙着转账呢!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()
+ ": 啊!险些误了大事");
}
}
public static void main(String[] args) throws InterruptedException {
MyRunnable target = new MyRunnable();
Thread thread = new Thread(target, "李四");
System.out.println(Thread.currentThread().getName()
+ ": 让李四开始转账。");
thread.start();
Thread.sleep(10 * 1000);
System.out.println(Thread.currentThread().getName()
+ ": 老板来电话了,得赶紧通知李四对方是个骗子!");
target.isQuit = true;
}
}
方法二: 使用 Thread.interrupted() Thread.currentThread().isInterrupted() 代替自定义标志位.
public class Dome2 {
public static class MyRunnable implements Runnable{
@Override
public void run(){
while (!Thread.interrupted()){
//while (!Thread.currentThread().isInterrupted())
System.out.println(Thread.currentThread().getName()+"别管我");
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
System.out.println(Thread.currentThread().getName()+"终止");
break;
}
}
System.out.println(Thread.currentThread().getName()+"还好");
}
}
public static void main(String[] args) throws InterruptedException {
MyRunnable target=new MyRunnable();
Thread thread=new Thread(target,"李四");
System.out.println(Thread.currentThread().getName()+"转账");
thread.start();
Thread.sleep(10*1000);
System.out.println(Thread.currentThread().getName()+"快停止");
thread.interrupt();
}
}
- 使用 Thread.isInterrupted() , 线程中断会清除标志位
- 使用 Thread.currentThread().isInterrupted() , 线程中断标记位不会清除。
三、线程等待
有时,我们需要等待一个线程完成它的工作后,才能进行自己的下一步工作。
public class Dome3 {
public static void main(String[] args) throws InterruptedException {
Runnable target = () -> {
try {
System.out.println(Thread.currentThread().getName() + "在忙");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "结束");
};
Thread thread1 = new Thread(target,"李四");
Thread thread2=new Thread(target,"王虎");
System.out.println("李四工作");
thread1.start();
thread1.join();
System.out.println("李四工作结束");
thread2.start();
thread2.join();
System.out.println("王虎工作结束");}
}
四、线程休眠
sleep( )使线程进入休眠转态。
public class Dome4 {
public static void main(String[] args) throws InterruptedException{
System.out.println(System.currentTimeMillis());
Thread.sleep(3*1000);
System.out.println(System.currentTimeMillis());
}
}
- 线程的调度是不可控的。
- sleep( )保证实际休眠时间是大于等于参数设置的休眠时间。
五、获取线程实例
- public static Thread currentThread(); 返回当前线程对象的引用
public class Dome5 {
public static void main(String[] args) {
Thread thread=Thread.currentThread();
System.out.println(thread.getName());
}
}