前言
记录时间:2023.3.18
已坚持的第二十天
java从入门到精通
学习java时间历程记录打卡
早上6:00到 12:00
下午1:00到 6: 00
完成代码练习
d1_create
1.多线程的创建方式一:继承Thread类实现
package cn.vqqc.d1_create;
/**
目标:多线程的创建方式一:继承Thread类实现
*/
public class ThreadDemo1 {
public static void main(String[] args) {
// 3、new一个新线程对象
Thread t = new MyThread();
// 4、调用start方法启动线程(执行的还是run方法)
t.start();
for (int i = 0; i < 5; i++) {
System.out.println("主线程执行输出:" + i);
}
}
}
/**
1、定义一个线程类继承Thread类
*/
class MyThread extends Thread{
/**
2、重写run方法,里面是定义线程以后要干啥
*/
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("子线程执行输出:" + i);
}
}
}
2.学会线程的创建方式二,理解它的优缺点
package cn.vqqc.d1_create;
/**
目标:学会线程的创建方式二,理解它的优缺点
*/
public class ThreadDemo2 {
public static void main(String[] args) {
// 3、创建一个任务对象
Runnable target = new MyRunnable();
// 4、把任务对象交给Thread处理
Thread t = new Thread(target);
// Thread t = new Thread(target, "1号");
// 5、启动线程
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("主线程执行输出:" + i);
}
}
}
/**
1、定义一个线程任务类 实现Runnable接口
*/
class MyRunnable implements Runnable {
/**
2、重写run方法,定义线程的执行任务的
*/
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("子线程执行输出:" + i);
}
}
}
3.学会线程的创建方式二(匿名内部类方式实现,语法形式)
package cn.vqqc.d1_create;
/**
目标:学会线程的创建方式二(匿名内部类方式实现,语法形式)
*/
public class ThreadDemo2Other {
public static void main(String[] args) {
Runnable target = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("子线程1执行输出:" + i);
}
}
};
Thread t = new Thread(target);
t.start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("子线程2执行输出:" + i);
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println("子线程3执行输出:" + i);
}
}).start();
for (int i = 0; i < 10; i++) {
System.out.println("主线程执行输出:" + i);
}
}
}
4.学会线程的创建方式三:实现Callable接口,结合FutureTask完成
package cn.vqqc.d1_create;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
目标:学会线程的创建方式三:实现Callable接口,结合FutureTask完成
*/
public class ThreadDemo3 {
public static void main(String[] args) {
// 3、创建Callable任务对象
Callable<String> call = new MyCallable(100);
// 4、把Callable任务对象 交给 FutureTask 对象
// FutureTask对象的作用1: 是Runnable的对象(实现了Runnable接口),可以交给Thread了
// FutureTask对象的作用2: 可以在线程执行完毕之后通过调用其get方法得到线程执行完成的结果
FutureTask<String> f1 = new FutureTask<>(call);
// 5、交给线程处理
Thread t1 = new Thread(f1);
// 6、启动线程
t1.start();
Callable<String> call2 = new MyCallable(200);
FutureTask<String> f2 = new FutureTask<>(call2);
Thread t2 = new Thread(f2);
t2.start();
try {
// 如果f1任务没有执行完毕,这里的代码会等待,直到线程1跑完才提取结果。
String rs1 = f1.get();
System.out.println("第一个结果:" + rs1);
} catch (Exception e) {
e.printStackTrace();
}
try {
// 如果f2任务没有执行完毕,这里的代码会等待,直到线程2跑完才提取结果。
String rs2 = f2.get();
System.out.println("第二个结果:" + rs2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
1、定义一个任务类 实现Callable接口 应该申明线程任务执行完毕后的结果的数据类型
*/
class MyCallable implements Callable<String>{
private int n;
public MyCallable(int n) {
this.n = n;
}
/**
2、重写call方法(任务方法)
*/
@Override
public String call() throws Exception {
int sum = 0;
for (int i = 1; i <= n ; i++) {
sum += i;
}
return "子线程执行的结果是:" + sum;
}
}
d2_api
1.定义类
package cn.vqqc.d2_api;
public class MyThread extends Thread{
public MyThread() {
}
public MyThread(String name) {
// 为当前线程对象设置名称,送给父类的有参数构造器初始化名称
super(name);
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println( Thread.currentThread().getName() + "输出:" + i);
}
}
}
2.线程的API
package cn.vqqc.d2_api;
/**
目标:线程的API
*/
public class ThreadDemo01 {
// main方法是由主线程负责调度的
public static void main(String[] args) {
Thread t1 = new MyThread("1号");
// t1.setName("1号");
t1.start();
System.out.println(t1.getName());
Thread t2 = new MyThread("2号");
// t2.setName("2号");
t2.start();
System.out.println(t2.getName());
// 哪个线程执行它,它就得到哪个线程对象(当前线程对象)
// 主线程的名称就叫main
Thread m = Thread.currentThread();
System.out.println(m.getName());
m.setName("最牛的线程");
for (int i = 0; i < 5; i++) {
System.out.println(m.getName() + "输出:" + i);
}
}
}
3.线程的API:休眠状态
package cn.vqqc.d2_api;
/**
目标:线程的API
*/
public class ThreadDemo02 {
// main方法是由主线程负责调度的
public static void main(String[] args) throws Exception {
for (int i = 1; i <= 5; i++) {
System.out.println("输出:" + i);
if(i == 3){
// 让当前线程进入休眠状态
Thread.sleep(3000);
}
}
}
}
d3_thread_safe
1.定义类
package cn.vqqc.d3_thread_safe;
public class Account {
private String cardId;
private double money; // 账户的余额
public Account(){
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
/**
小明 小红
*/
public void drawMoney(double money) {
// 0、先获取是谁来取钱,线程的名字就是人名
String name = Thread.currentThread().getName();
// 1、判断账户是否够钱
if(this.money >= money){
// 2、取钱
System.out.println(name + "来取钱成功,吐出:" + money);
// 3、更新余额
this.money -= money;
System.out.println(name + "取钱后剩余:" + this.money);
}else {
// 4、余额不足
System.out.println(name +"来取钱,余额不足!");
}
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
2.取钱的线程类
package cn.vqqc.d3_thread_safe;
/**
取钱的线程类
*/
public class DrawThread extends Thread {
// 接收处理的账户对象
private Account acc;
public DrawThread(Account acc,String name){
super(name);
this.acc = acc;
}
@Override
public void run() {
// 小明 小红:取钱
acc.drawMoney(100000);
}
}
3.模拟取钱案例
package cn.vqqc.d3_thread_safe;
/**
需求:模拟取钱案例
*/
public class ThreadDemo {
public static void main(String[] args) {
// 1、定义线程类,创建一个共享的账户对象
Account acc = new Account("ICBC-111", 100000);
// 2、创建2个线程对象,代表小明和小红同时进来了。
new DrawThread(acc, "小明").start();
new DrawThread(acc, "小红").start();
}
}
d4_thread_synchronized_code
1.定义类
package cn.vqqc.d4_thread_synchronized_code;
/**
账户类:余额,卡号
*/
public class Account {
private String cardId;
private double money; // 余额 关键信息
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
// // 100个线程人
// public static void run(){
// synchronized (Account.class){
//
// }
// }
/**
小明 小红
*/
public void drawMoney(double money) {
// 1、拿到是谁来取钱
String name = Thread.currentThread().getName();
// 同步代码块
// 小明 小红
// this == acc 共享账户
synchronized (this) {
// 2、判断余额是否足够
if(this.money >= money){
// 钱够了
System.out.println(name+"来取钱,吐出:" + money);
// 更新余额
this.money -= money;
System.out.println(name+"取钱后,余额剩余:" + this.money);
}else{
// 3、余额不足
System.out.println(name+"来取钱,余额不足!");
}
}
}
}
2.线程类
package cn.vqqc.d4_thread_synchronized_code;
/**
线程类
*/
public class DrawThread extends Thread{
private Account acc;
public DrawThread(Account acc, String name){
super(name);
this.acc = acc;
}
@Override
public void run() {
// 小明 小红 : acc
acc.drawMoney(100000);
}
}
3.测试线程安全问题
package cn.vqqc.d4_thread_synchronized_code;
public class TestSafeDemo {
public static void main(String[] args) {
// 测试线程安全问题
// 1、创建一个共享的账户对象。
Account acc = new Account("ICBC-111" , 100000);
// 2、创建2个线程对象,操作同一个账户对象
new DrawThread(acc, "小明").start();
new DrawThread(acc,"小红").start();
}
}
d5_thread_synchronized_method
1.定义类
package cn.vqqc.d5_thread_synchronized_method;
/**
账户类:余额,卡号
*/
public class Account {
private String cardId;
private double money; // 余额 关键信息
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
/**
小明 小红
this == acc
*/
public synchronized void drawMoney(double money) {
// 1、拿到是谁来取钱
String name = Thread.currentThread().getName();
// 2、判断余额是否足够
// 小明 小红
if(this.money >= money){
// 钱够了
System.out.println(name+"来取钱,吐出:" + money);
// 更新余额
this.money -= money;
System.out.println(name+"取钱后,余额剩余:" + this.money);
}else{
// 3、余额不足
System.out.println(name+"来取钱,余额不足!");
}
}
}
2.线程类
package cn.vqqc.d5_thread_synchronized_method;
/**
线程类
*/
public class DrawThread extends Thread{
private Account acc;
public DrawThread(Account acc, String name){
super(name);
this.acc = acc;
}
@Override
public void run() {
// 小明 小红 : acc
acc.drawMoney(100000);
}
}
3.测试线程安全问题2:this
package cn.vqqc.d5_thread_synchronized_method;
import java.util.Hashtable;
public class TestSafeDemo {
public static void main(String[] args) {
// 测试线程安全问题
// 1、创建一个共享的账户对象。
Account acc = new Account("ICBC-111" , 100000);
// 2、创建2个线程对象,操作同一个账户对象
new DrawThread(acc, "小明").start();
new DrawThread(acc,"小红").start();
}
}
视频总结
1.线程概述、线程创建方式一
https://www.bilibili.com/video/BV1Cv411372m?p=168
2.线程创建方式二、线程创建方式三
https://www.bilibili.com/video/BV1Cv411372m?p=169
3.线程常用方法
https://www.bilibili.com/video/BV1Cv411372m?p=170
4.线程安全问题,模拟线程安全问题案例:取款
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容