一、进程vs线程
(1)进程是资源分配的最小单位
(2)线程是CPU调度的最小单位
二、创建线程
方法一:继承Thread类
public class ThreadDemo{
private static class myThread extends Thread{
@orride
public void run(){
//这里是线程运行代码
}
}
public static void main(String[] args){
Thread t = new myThread();
t.start() //线程开始
}
}
方法二:实现Runnable接口
实现Runnable接口,通过Thread构造方法将Runnable对象作为(任务)target参数传入线程对象中
public ThreadDemo{
private static class myRunnable implements Runnable{
@orride
public void run(){
System.out.println(Thread.currentThread().getName() + "这里是线程运行代码");
}
}
public static void main(String[] args){
Thread t = new Thread(new myRunnable());
t.start();
}
}
方法三:实现Callable接口
(1)创建一个类并实现Callable接口
(2)重写call()方法,将所要完成的任务的代码写进call()方法中,call()方法有返回值,并且可以抛出异常
(3)如果想要获取运行该线程后的返回值,需要创建Future接口的实现类的对象,即FutureTask类的对象,调用该对象的get()方法可获取call()方法的返回值
(4)使用Thread类的有参构造器创建对象,将FutureTask类的对象当做参数传进去,然后调用start()方法开启并运行该线程。
public class ThreadDemo {
public static void main(String[] args) throws Exception {
//创建FutureTask的对象
FutureTask<String> task = new FutureTask<String>(new myCallable());
//创建Thread类的对象
Thread t = new Thread(task);
//开启线程
t.start();
//获取call()方法的返回值,即线程运行结束后的返回值
String result = task.get();
System.out.println(result);
}
}
class myCallable implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName()+":"+"输出的结果");
return Thread.currentThread().getName()+":"+"返回的结果";
}
}
方法四:使用线程池创建
(1)使用Executors类中的newFixedThreadPool(int num)方法创建一个线程数量为num的线程池
(2)调用线程池中的==execute()方法执行由实现Runnable接口创建的线程;调用submit()方法执行由实现Callable接口创建的线程
(3)调用线程池中的shutdown()==方法关闭线程池
package 创建线程;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ThreadDemo {
public static void main(String[] args) throws Exception{
//通过线程池工厂创建线程数量为2 的线程池
ExecutorService pool = Executors.newFixedThreadPool(2);
//利用Execute()方法实现Runnable接口创建的线程
pool.execute(new Thread1());
pool.execute(new Thread2());
pool.execute(new Thread3());
//利用submit()实现callable接口创建线程
Future<String> task = pool.submit(new Thread4());
//获取call()的返回值
String result = task.get();
System.out.println(result);
//关闭线程
pool.shutdown();
}
}
//实现Runnable接口
class Thread1 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+"输出的结果");
}
}
class Thread2 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+"输出的结果");
}
}
class Thread3 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+"输出的结果");
}
}
//实现Callable接口
class Thread4 implements Callable<String>{
@Override
public String call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+"输出的结果");
return Thread.currentThread().getName()+":"+"返回的结果";
}
}
三、Thread类常见的方法
1、常见的构造方法
(1)Thread() :创建线程对象
(2)Thread(Runnable target) :使用Runnable对象创建线程对象
(3)Thread(String name):创建线程对象,并命名
(4)Thread(Runnable target,String name):使用Runnable对象创建线程对象,并命名
2、常见的属性获取
getId():获取ID;ID是线程的唯一标识,不同线程不会重复
getName():线程名称
getState():线程状态
getPriority():线程优先级
isDaemon():是否是后台线程
isAlive():线程是否存活
isInterrupted():线程是否被中断
3、start()启动一个线程
调用start():
(1)线程状态发生变化:NEW ---->RUNNABLE由创建变为就绪,线程拥有了抢CPU的资格,
(2)调用完start()后,线程并没有开始执行,当线程被调度到CPU上是才开始真正执行
run()方法和start()方法的区别:
(1)覆写run()方法是提供给线程要做的事情的指令清单
(2)调用start(),线程开始有抢CPU的资格
4、中断一个线程
**interrupt():**中断线程
boolean isInterrupted(): t.isInterrupted()判断t线程是否中断
**static boolean Interrupted()😗*判断当前线程是否中断
方法一:通过共享标记进行沟通
public class Demo1 {
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 t1 = new Thread(target,"李四");
System.out.println(Thread.currentThread().getName() + ":让李四开始转账");
t1.start();
Thread.sleep(10 * 1000);
System.out.println(Thread.currentThread().getName()+ ":老板来电话了,通知李四是个骗子!");
target.isQuit = true;
}
}
方法二:调用interrupt()方法来通知
public class Demo {
private static class MyRunnable implements Runnable {
@Override
public void run() {
while (!Thread.interrupted()) {
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 t2 = new Thread(target,"李四");
System.out.println(Thread.currentThread().getName() + ":让李四开始转账");
t2.start();
Thread.sleep(10 * 1000);
System.out.println(Thread.currentThread().getName() + ":通知李四对方是个骗子");
t2.interrupt();
}
}
说明:
1、第二种方法通过调用interrupt()方法通知线程中断
2、thread收到通知的方法有两种:
(1)如果线程调用了wait/join/sleep等方法阻塞挂起,则以InterruptedExceotion异常形式通知,清除中断标志。
(2)否则,只是内部的一个中断标志被设置
(1)Thread通过interrupted()判断当前线程的中断被标志,清除中断标志
(2)通过isInterrupted()判断指定线程的中断标志,不清除中断标志
5、join()等待一个线程
t.join():当调用线程执行到该语句时,当前的“调用线程”会从CPU上调度下来,并放弃抢CPU的资格;直到t这个线程执行结束,“调用线程”才拥有抢CPU的资格
6、获取当前线程
Thread.currentThread()
7、休眠当前线程
Thread.sleep()
8、yield()方法
让出CPU,重新排到就绪队列中