java多线程编程入门

概念

即异步编程

创建线程

继承Thread对象,重写run方法

image

实现Runnable接口

可以使用lambda表达式简化

image

返回结果的线程,实现Callable接口

images

线程安全

定义

多个线程同时访问同一共享资源造成错误的现象

线程锁synchronized

通常使用this指针作为同步锁,静态函数常使用:类.class

image
image

上文的锁属于悲观锁,乐观锁只锁定程序出现访问冲突的个别回合,如AtomicInteger

线程通信

image

线程池

image
image

线程的生命周期

image
image

posted @ 2023-10-06 03:08  adamaik  阅读(12)  评论(0)    收藏  举报