来源:http://www.bjsxt.com/
一、S02E180_01线程_死锁
package com.test.thread.syn;
/**
* 过多的同步容易造成死锁
*/
public class Deadlock {
public static void main(String[] args) {
Object g = new Object();
Object m = new Object();
//使用同一份资源g,m,互相不释放则死锁
Test1 t1 = new Test1(g,m);
Test2 t2 = new Test2(g,m);
Thread proxy1 = new Thread(t1);
Thread proxy2 = new Thread(t2);
proxy1.start();
proxy2.start();
}
}
/**
* 先给货再给钱
*/
class Test1 implements Runnable{
Object goods;
Object money;
public Test1() {
}
public Test1(Object goods, Object money) {
super();
this.goods = goods;
this.money = money;
}
@Override
public void run() {
while(true){
test();
}
}
public void test(){
synchronized(goods){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(money){
}
}
System.out.println("先给货再给钱");
}
}
/**
* 先给钱再给货
*/
class Test2 implements Runnable{
Object goods;
Object money;
public Test2() {
}
public Test2(Object goods, Object money) {
super();
this.goods = goods;
this.money = money;
}
@Override
public void run() {
while(true){
test2();
}
}
public void test2(){
synchronized(money){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(goods){
}
}
System.out.println("先给钱再给货");
}
}
二、S02E181_01线程_生产者消费者模式(信号灯法)
生产者消费者模式不是设计模式(讲的是类与类之间的关系),是解决多线程同步容易造成死锁问题的方案
package com.test.thread.syn;
/**
* 生产者
*/
public class Player implements Runnable{
private Movie m;
public Player(Movie m) {
super();
this.m = m;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if(0==i%2){
m.play("左青龙");
}else{
m.play("右白虎");
}
}
}
}
package com.test.thread.syn;
/**
* 消费者
*/
public class Watcher implements Runnable{
private Movie m;
public Watcher(Movie m) {
super();
this.m = m;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
m.watch();
}
}
}
package com.test.thread.syn;
/**
* 一个场景,共同的资源
* 生产者消费者模式(信号灯法)
* 与synchronized一起使用
* wait():等待,释放锁 sleep():不释放锁
* notify()/notifyAll():唤醒
*/
public class Movie {
private String pic;
//信号灯
//flag -->T 生产者生产,消费者等待,生产完成后通知消费
//flag -->F 消费者消费,生产者等待,消费完成后通知生产
private boolean flag = true;
/**
* 播放
*/
public synchronized void play(String pic){
if(!flag){//生产者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始生产
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产了:" + pic);
//生产完毕
this.pic = pic;
//通知消费
this.notify();
//生产者停下
this.flag = false;
}
/**
* 查看
*/
public synchronized void watch(){
if(flag){//消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始消费
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//消费完毕
System.out.println("消费了:" + pic);
//通知生产
this.notifyAll();
//消费停止
this.flag = true;
}
}
package com.test.thread.syn;
public class MovieApp {
public static void main(String[] args) {
//共同的资源
Movie m = new Movie();
//多线程
Player p = new Player(m);
Watcher w = new Watcher(m);
new Thread(p).start();
new Thread(w).start();
}
}
三、S02E182_01线程_任务调度
package com.test.thread.schedule;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Timer()
* void schedule(TimerTask task, Date time)
安排在指定的时间执行指定的任务。
void schedule(TimerTask task, Date firstTime, long period)
安排指定的任务在指定的时间开始进行重复的固定延迟执行。
* 自学Quartz框架:http://www.quartz-scheduler.org/
*/
public class TimerDemo {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("SO EASY...");
}
}, new Date(System.currentTimeMillis() + 1000), 500);
}
}
线程控制与调度
本文介绍了Java中线程控制的基本概念,包括死锁的产生原因及预防措施,通过生产者消费者模式实现线程间的协作,并展示了如何利用定时任务实现周期性的操作。
2024

被折叠的 条评论
为什么被折叠?



