千锋逆战班:孙华建
在千锋学习第29天
“未来的你会感谢今天奋斗的自己”
今天我学习了java课程,同步方法,死锁问题,线程通信,线程池概念,原理,Callable接口,Future接口,线程同步,异步
#中国加油!武汉加油!千锋加油!也为自己加油!!!#…
笔记:
课堂案例:
1,死锁
public class TestDeadLock {
public static void main(String[] args) {
LeftChopstick left = new LeftChopstick();
RightChopstick right = new RightChopstick();
Thread boy = new Thread(new Boy(left,right));
Thread girl = new Thread(new Girl(left,right));
Thread baby = new Thread(new Baby(left,right));
boy.start();
girl.start();
baby.start();
}
}
class LeftChopstick{
String name = "左筷子";
}
class RightChopstick{
String name = "右筷子";
}
class Boy implements Runnable{
LeftChopstick left;
RightChopstick right;
public Boy(LeftChopstick left, RightChopstick right) {
super();
this.left = left;
this.right = right;
}
public void run() {
System.out.println("男孩要拿筷子!");
synchronized(left){//拿到做筷子资源,加锁!
//让出筷子,让老婆孩子先吃
try {
left.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("男孩拿到了左筷子,开始拿右筷子");
synchronized(right){//拿到右筷子资源,加锁!
System.out.println("男孩子拿到了右筷子,开始吃饭");
}
}
}
}
class Girl implements Runnable{
LeftChopstick left;
RightChopstick right;
public Girl(LeftChopstick left, RightChopstick right) {
super();
this.left = left;
this.right = right;
}
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("女孩要拿筷子!");
synchronized(right){//拿到右筷子资源,加锁!
System.out.println("女孩拿到了右筷子,开始拿左筷子");
synchronized(left){//拿到左筷子资源,加锁!
System.out.println("女孩子拿到了左筷子,开始吃饭");
}
//女孩吃完后,唤醒等待右筷子锁的男孩和宝宝
right.notify();
}
}
}
class Baby implements Runnable{
LeftChopstick left;
RightChopstick right;
public Baby(LeftChopstick left, RightChopstick right) {
super();
this.left = left;
this.right = right;
}
public void run(){
System.out.println("宝宝要拿筷子!");
synchronized(right){//拿到右筷子资源,加锁!
//宝宝让妈妈先吃
try {
right.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("宝宝拿到了右筷子,开始拿左筷子");
synchronized(left){//拿到左筷子资源,加锁!
System.out.println("宝宝拿到了左筷子,开始吃饭");
//宝宝吃完,把左筷子给爸爸,让爸爸吃饭
left.notify();
}
}
}
}
2,生产者,消费者
public class TestProduatCustomer {
public static void main(String[] args) {
//创建共同资源
Shop shop = new Shop();
Thread p = new Thread(new Product(shop));
Thread c = new Thread(new Costomer(shop));
p.start();
c.start();
}
}
//商品
class Goods{
private int id;
public Goods(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//商场
class Shop{
Goods goods;
boolean flag;//表示商品是否充足;
//生产者调用存的方法
public synchronized void saveGoods(Goods goods) throws InterruptedException{
//判断商品是否充足
if(flag == true){
System.out.println("生产者:商品充足!要等待了");
this.wait();//商品充足,生产者等待生产
}
//商品不充足,生产者生产商品,存入商场
System.out.println(Thread.currentThread().getName()+"生产者往商场里存放了"+goods.getId()+"件商品");
this.goods = goods;
flag = true;//已经有商品,可以让消费者来购买了
this.notifyAll();//将等待队列的消费者唤醒,前来购买商品
}
//消费者调用取的方法
public synchronized void buyGoods() throws InterruptedException{
if(flag == false){//没有商品了,消费者等待购买
System.out.println("消费者:商品不充足,要等待了");
this.wait();
}
//正常购买商品
System.out.println(Thread.currentThread().getName()+"购买了"+goods.getId()+"件商品");
//商品买完,标识没货了
this.goods = null;
flag = false;
//唤醒生产者去生产商品
this.notifyAll();
}
}
//生产者
class Product implements Runnable{
Shop shop;
public Product(Shop shop){
this.shop = shop;
}
public void run() {
//通过循环,将商品存入商场
for(int i = 1;i<=30;i++){
try {
this.shop.saveGoods(new Goods(i));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//消费者
class Costomer implements Runnable{
Shop shop;
public Costomer(Shop shop){
this.shop = shop;
}
public void run() {
for(int i = 1;i <= 30;i++){
try {
this.shop.buyGoods();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3,使用两个线程,异步运算,计算出1-50和51-100相加的和,再进行汇总
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestFuture {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService es = Executors.newFixedThreadPool(2);
MyCall mc = new MyCall();
MyCall2 mc2 = new MyCall2();
Future<Integer> result =es.submit(mc);
Future<Integer> result2 =es.submit(mc2);
Integer value =result.get();
Integer value2 =result2.get();
System.out.println(value+value2);
}
}
class MyCall implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("MyCall正在执行。。。");
Integer sum = 0;
for(int i = 1;i <= 50;i++){
sum = sum + i;
}
return sum;
}
}
class MyCall2 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("MyCall2正在执行。。。");
Integer sum = 0;
for(int i = 51;i<=100;i++){
sum = sum + i;
}
return sum;
}
}
作业:
C
代表对System.out这个对象加锁,两者持有统一对象,所以可以
还有一处,MyThread2中,run方法不能声明异常