目录
java多线程的几种实现方式:
1.继承Thread类,重写run方法
2.实现Runnable接口,重写run方法
3.通过Callable接口
4.通过线程池创建线程
注意:线程推荐创建方式有两种继承Thread类和实现Runnable接口
线程安全
线程的状态
java多线程的几种实现方式:
1.继承Thread类,重写run方法
- 继承Thread类,实现run方法
- 创建线程对象
- 线程对象调用start方法
public class Test extends Thread{
//通过继承Thread类的方式实现多线程
public static void main(String[] args) {
CreateThread createThread = new CreateThread();
createThread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("线程");
}
}
}
class CreateThread extends Thread{
@Override
public void run(){
for (int i = 0; i <1000 ; i++) {
System.out.println(i);
}
}
}
2.实现Runnable接口,重写run方法
- 实现Runnable接口,重写run方法
- 创建Runnable接口实现类
- 执行线程需要丢入Runnable接口实现类
- 调用start方法
public class Test01 {
//通过实现Runnable接口实现线程
public static void main(String[] args){
System.out.println(Thread.currentThread().getName());
Thread t1 = new Thread(new MyThread());
t1.start();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->通过实现接口的线程实现方式!");
}
}
3.通过Callable接口
- 实现Callable接口需要返回值类型
- 重写call方法,需要抛出异常
- 创建目标对象
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class TicketWindow implements Callable
4.通过线程池创建线程
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test02 {
private static int POOL_NUM = 10; //线程池数量
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i
注意:线程推荐创建方式有两种继承Thread类和实现Runnable接口
线程安全
什么情况下产生线程安全问题:多个线程访问同一资源,有可能产生线程安全问题
解决线程安全问题的三种方式:
synchronized代码块:
语法:
synchronized(锁对象){
//有可能发生线程安全问题的代码
}
synchronized方法:synchronized关键字写在返回值之前
静态:static修饰的方法。锁对象:当前类的反射对象。 Class 对象名 = 类名.class;
非静态:非static修饰的方法。锁对象:this
Lock接口:
void lock();
void unlock();
常用的实现类:ReentrantLock
构造方法:
public ReentrantLock();
synchronized:执行完大括号中的代码后,会自动释放锁。
synchronized代码块或者方法中发生异常后会不会释放锁对象:会释放锁对象
Lock:必须手动的调用unlock方法才能释放锁。
Lock锁,发生异常后不会自动释放,所以建议把手动释放锁的代码写在finally块中
线程的状态
NEW:新建状态。一个线程创建以后,启动之前,就处于该状态。
TERMINATED:消亡状态。线程执行完任务后,处于该状态。
RUNNABLE:可运行状态。线程正在整型任务,就处于该状态
BLOCKED:阻塞状态。获取synchronized锁对象失败,处于该状态
WAITING:无限等待状态。获取Lock锁对象失败,就处于该状态
TIMED_WAITING:计时等待状态。执行sleep方法,就处于该状态。
- 实现Runnable接口,重写run方法
- 创建Runnable接口实现类
- 执行线程需要丢入Runnable接口实现类
- 调用start方法
public class Test01 {
//通过实现Runnable接口实现线程
public static void main(String[] args){
System.out.println(Thread.currentThread().getName());
Thread t1 = new Thread(new MyThread());
t1.start();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->通过实现接口的线程实现方式!");
}
}
3.通过Callable接口
- 实现Callable接口需要返回值类型
- 重写call方法,需要抛出异常
- 创建目标对象
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class TicketWindow implements Callable
4.通过线程池创建线程
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test02 {
private static int POOL_NUM = 10; //线程池数量
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i
注意:线程推荐创建方式有两种继承Thread类和实现Runnable接口
线程安全
什么情况下产生线程安全问题:多个线程访问同一资源,有可能产生线程安全问题
解决线程安全问题的三种方式:
synchronized代码块:
语法:
synchronized(锁对象){
//有可能发生线程安全问题的代码
}
synchronized方法:synchronized关键字写在返回值之前
静态:static修饰的方法。锁对象:当前类的反射对象。 Class 对象名 = 类名.class;
非静态:非static修饰的方法。锁对象:this
Lock接口:
void lock();
void unlock();
常用的实现类:ReentrantLock
构造方法:
public ReentrantLock();
synchronized:执行完大括号中的代码后,会自动释放锁。
synchronized代码块或者方法中发生异常后会不会释放锁对象:会释放锁对象
Lock:必须手动的调用unlock方法才能释放锁。
Lock锁,发生异常后不会自动释放,所以建议把手动释放锁的代码写在finally块中
线程的状态
NEW:新建状态。一个线程创建以后,启动之前,就处于该状态。
TERMINATED:消亡状态。线程执行完任务后,处于该状态。
RUNNABLE:可运行状态。线程正在整型任务,就处于该状态
BLOCKED:阻塞状态。获取synchronized锁对象失败,处于该状态
WAITING:无限等待状态。获取Lock锁对象失败,就处于该状态
TIMED_WAITING:计时等待状态。执行sleep方法,就处于该状态。
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class TicketWindow implements Callable
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test02 {
private static int POOL_NUM = 10; //线程池数量
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i
注意:线程推荐创建方式有两种继承Thread类和实现Runnable接口
线程安全
什么情况下产生线程安全问题:多个线程访问同一资源,有可能产生线程安全问题
解决线程安全问题的三种方式:
synchronized代码块:
语法:
synchronized(锁对象){
//有可能发生线程安全问题的代码
}
synchronized方法:synchronized关键字写在返回值之前
静态:static修饰的方法。锁对象:当前类的反射对象。 Class 对象名 = 类名.class;
非静态:非static修饰的方法。锁对象:this
Lock接口:
void lock();
void unlock();
常用的实现类:ReentrantLock
构造方法:
public ReentrantLock();
synchronized:执行完大括号中的代码后,会自动释放锁。
synchronized代码块或者方法中发生异常后会不会释放锁对象:会释放锁对象
Lock:必须手动的调用unlock方法才能释放锁。
Lock锁,发生异常后不会自动释放,所以建议把手动释放锁的代码写在finally块中
线程的状态
NEW:新建状态。一个线程创建以后,启动之前,就处于该状态。
TERMINATED:消亡状态。线程执行完任务后,处于该状态。
RUNNABLE:可运行状态。线程正在整型任务,就处于该状态
BLOCKED:阻塞状态。获取synchronized锁对象失败,处于该状态
WAITING:无限等待状态。获取Lock锁对象失败,就处于该状态
TIMED_WAITING:计时等待状态。执行sleep方法,就处于该状态。



