X04多线程部分01
- 线程不安全案例
- 生产者消费者模式
- 同步锁的使用
- 数据定时备份
- 总目录
线程不安全案例
package ksdxc.d19;
// 线程不安全案例:线程不安全,有负数
public class d19 {
public static void main(String[] args) {
BuyTicket customer = new BuyTicket();
new Thread(customer,"a").start();
new Thread(customer,"b").start();
new Thread(customer,"c").start();
}
}
class BuyTicket implements Runnable{
private int ticketNum = 10;
private boolean flag = true;
@Override
public void run() {
while (flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void buy() throws InterruptedException{
if (ticketNum <= 0){
flag = false;
return;
}
// 延时
Thread.sleep(120);
System.out.println(Thread.currentThread().getName());
ticketNum--;
}
}
package ksdxc.d19;
public class bank {
public static void main(String[] args) {
Account account = new Account(66,"pool");
Withdrawal withdrawal1 = new Withdrawal(account,55,"coding");
Withdrawal withdrawal2 = new Withdrawal(account,55,"walking");
withdrawal1.start();
withdrawal2.start();
}
}
class Account{
int remainMoney; // 余额
String cardName; // 银行卡类别
public Account(int remainMoney, String cardName) {
this.remainMoney = remainMoney;
this.cardName = cardName;
}
}
class Withdrawal extends Thread{
Account account;
int drawMone; // 取了多少钱
String threadName; // 现金
public Withdrawal(Account account,int drawMone,String threadName){
super(threadName);
this.account = account;
this.drawMone = drawMone;
}
@Override
public void run() {
// 判断有没有前
if (account.remainMoney - drawMone < 0){
System.out.println(Thread.currentThread().getName() + "operator fail");
return;
}
try {
Thread.sleep(120);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 程序能够执行到这里说明取款成功
account.remainMoney = account.remainMoney - drawMone;
System.out.println(Thread.currentThread().getName());
System.out.println(account.remainMoney);
}
}
生产者消费者模式
package du;
import java.util.ArrayList;
import java.util.List;
public class d802 {
public static void main(String[] args) {
List list = new ArrayList<>();
Thread thread1 = new Thread(new Productor(list));
Thread thread2 = new Thread(new Consumer(list));
thread1.setName("生产者线程");
thread2.setName("消费者线程");
thread1.start();
thread2.start();
}
}
class Productor implements Runnable{
private List list;
public Productor(List list){
this.list = list;
}
@Override
public void run() {
while (true){
synchronized (list){
if (list.size() > 0){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object o = new Object();
list.add(o);
System.out.println(Thread.currentThread().getName() + " " + o);
list.notifyAll();
}
}
}
}
class Consumer implements Runnable{
private List list;
public Consumer(List list){
this.list = list;
}
@Override
public void run() {
while (true){
synchronized (list){
if (list.size() == 0){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object remove = list.remove(0);
System.out.println(Thread.currentThread().getName() + " " + remove);
list.notifyAll();
}
}
}
}
同步锁的使用
数据定时备份
package du;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class d800 {
public static void main(String[] args) throws Exception {
Timer timer = new Timer();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstExecTime = simpleDateFormat.parse("2021-09-27 20:05:00");
timer.schedule(new LogTimerTask(),firstExecTime,1000*5);
}
}
class LogTimerTask extends TimerTask {
@Override
public void run() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string = simpleDateFormat.format(new Date());
System.out.println(string + "finish data archived");
}
}
总目录