ok了家人们今天学习多线程,
一.多线程相关概念
1.1 并行与并发
并行:在同一时刻,有多个任务在多个
CPU
上同时执行。
并发:在同一时刻,有多个任务在单个
CPU
上交替执行。
1.2 多线程
cpu同时执行多个程序。
好处
:
提高程序的效率。
二.多线程创建方式
2.1 创建方式一:继承Thread方式
Thread
是线程类,有两个方法:
public void run() : 线程执行任务的方法,是线程启动后第一个执
行的方法
public void start() : 启动线程的方法, 线程对象调用该方法后,Java虚
拟机就会调用此线程的run方法。
package 多线程Thread;
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+"HelloWorld");
}
}
}
package 多线程Thread;
public class ThreadTest {
public static void main(String[] args) {
MyThread th01 = new MyThread();
MyThread th02 = new MyThread();
th01.setName("线程1");
th01.start();
th02.setName("线程2");
th02.start();
}
}
2.2 创建方式二:实现Runable接口方式
第二种创建方式使用接口Runnable,重写接口Runnable ,run方法
创建任务对象
使用含有
Runnable
参数的构造方法,创建线程对象并指定任
务。
调用线程的
start
方法,开启线程。
public class Mydesk implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程1-" + i);
}
}
}
public class DeskTest {
public static void main(String[] args) {
Mydesk md = new Mydesk();
Thread th = new Thread(md);
th.start();
for (int i = 0; i < 200; i++) {
System.out.println("线程2-"+i);
}
}
}
2.3 创建方式三:实现多线程的优先级
获取类的优先级和指定优先级
方法
package 多线程线程的优先级setPriority;
public class ThreadTest {
public static void main(String[] args) {
MyThread mt = new MyThread();
Thread td = new Thread(mt,"飞机");//优先级为5
Thread td01 = new Thread(mt,"tank");//优先级为1-10
System.out.println(td.getPriority());
System.out.println(td01.getPriority());
td.setPriority(10);
td01.setPriority(1);
td.start();
td01.start();
}
}
三.Thread类常用方法
获取线程的名字:String getName()
设置线程的名字:void setName(String name);通过构造方法
也可以设置线程名称
获得当前线程的对象:public static Thread currentThread()
让线程休眠指定的时间,单位为毫秒:public static void
sleep(long time)
public class Mydesk implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + i);//获取名字
}
}
}
public class dm02 {
public static void main(String[] args) throws InterruptedException {
Mydesk md = new Mydesk();
Thread th = new Thread(md,"线程1");//设置名字
th.start();
//Thread.sleep(1000);
for (int i = 0; i < 200; i++) {
System.out.println("线程2-"+i);
}
}
}
四.线程安全问题
4.1 线程安全问题演示
package 综合案例_多线程SellTicket;
public class Selldesk implements Runnable {
private int ticket = 100;
@Override
public void run() {
while (true) {
if (ticket <= 0) {
break;
}
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在卖" + ticket + "号票");
ticket--;
}
}
}
}
package 综合案例_多线程SellTicket;
public class SellTest {
public static void main(String[] args) {
Selldesk sk = new Selldesk();
Thread th = new Thread(sk,"窗口一-");
Thread th02 = new Thread(sk,"窗口二-");
Thread th03 = new Thread(sk,"窗口三-");
th.start();
th02.start();
th03.start();
}
}
卖票出现了问题

问题原因:多个线程在对共享数据进行读改写的时候,可能导致的
数据错乱就是线程的安全问题了
4.2 卖票案例数据安全问题的解决
基本思想:让共享数据存在安全的环境中
,
当某一个线程访问共享数
据时 其他线程是无法操作的
把多条线程操作共享数据的代码给锁起来,让任意时刻只能有一个
线程执行即可。
Java
提供了同步代码块的方式来解决。
4.2.1 同步代码块
默认情况锁是打开的,只要有一个线程进去执行代码了,锁就会
关闭
当线程执行完出来了,锁才会自动打开
锁对象可以是任意对象
,
但是多个线程必须使用同一把锁
synchronized(任意对象) {
多条语句操作共享数据的代码
}
同步的好处和弊端
好处:解决了多线程的数据安全问题
弊端:当线程很多时,因为每个线程都会去判断同步上的锁,这
是很耗费资源的,无形中会降低程序的运行效率
package 综合案例_多线程SellTicket;
public class Selldesk implements Runnable {
private int ticket = 100;
Object obj = new Object();
@Override
public void run() {
while (true) {
if (ticket <= 0) {
break;
}
synchronized (obj) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在卖" + ticket + "号票");
ticket--;
}
}
}
}
}
4.2.2 同步方法
同步方法:就是把
synchronized
关键字加到方法上
,
保证线程执行该
修饰符
synchronized
返回值类型
方法名
(
方法参数
) { }
同步代码块可以锁住指定代码
,
同步方法是锁住方法中所有代码
同步代码块可以指定锁对象
,
同步方法不能指定锁对象
注意
:
同步方法时不能指定锁对象的
,
但是有默认存在的锁对象的。
对于非
static
方法
,
同步锁就是
this
。
对于
static
方法
,
我们使用当前方法所在类的字节码对象
(
类
名
.class)
。
Class
类型的对象
package 综合案例_多线程SellTicket;
public class Selldesk implements Runnable {
private int ticket = 100;
Object obj = new Object();
@Override
public void run() {
while (true) {
if (ticket <= 0) {
break;
}
method();
}
}
public synchronized void method(){
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在卖" + ticket + "号票");
ticket--;
}
}
}
4.2.3 Lock锁机制
虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们
并没有直接看到在哪里加上了锁,在哪里释放了锁,为了更清晰的
表达如何加锁和释放锁,
JDK5
以后提供了一个新的锁对象
Lock
Lock
中提供了获得锁和释放锁的方法
void lock():获得锁
void unlock():释放锁
Lock是接口不能直接实例化,这里采用它的实现类ReentrantLock
来实例化
ReentrantLock的构造方法ReentrantLock():创建一个
ReentrantLock的实例
注意:多个线程使用相同的
Lock
锁对象,需要多线程操作数据的代
码放在
lock()
和
unLock()
方法之间。一定要确保
unlock
最后能够调用
package 综合案例_多线程SellTicket;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Selldesk implements Runnable {
private int ticket = 100;
Object obj = new Object();
Lock l = new ReentrantLock();
@Override
public void run() {
while (true) {
if (ticket <= 0) {
break;
}
l.lock();
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在卖" + ticket + "号票");
ticket--;
}
l.unlock();
}
}
}
ok了家人们,多线程搞完了。