Java入门11--多线程

====================
1.多线程
工人吃馒头的问题

public class t1 {
    public static void main(String[] args){
        Basket basket = new Basket();
        for(int i = 0; i <= 40 ; i ++){
            new Worker("Worlk-" + i , basket).start();
        }

    }
}

class Worker extends Thread{
    private String name;
    private static int MAX = 3;//定义工人最多吃几个馒头
    private int count;
    private Basket basket;
    public Worker(String name,Basket basket){
        this.name = name;
        this.basket = basket;
   //     this.count = count;
    }
    public void run(){
        while (true){
            //条件1:判断是否已经吃饱
            if(count >= MAX){
                return;
            }
            //2.去取馒头
            int no = basket.getMantou();
            if(no == 0){
                return ;
            }
            //拿到了馒头
            count ++ ;
            System.out.println(name + " : " + no);
        }
    }
}

//篮子
class Basket{
    private int count = 100;

    //同步方法,以当前对象作为锁旗标
    public synchronized int getMantou(){
        int temp = count;
        count -- ;
        return temp > 0 ? temp : 0 ;
    }
}

在这里插入图片描述
=========================
2.

蜜蜂和熊的问题
蜂蜜罐子存储
蜜蜂一次生产1个
熊一次吃20个
public class t2 {
    public static void main(String[] args){
        Box box = new Box();
        for(int i = 1;i <= 100 ; i ++){
            new Bee("Bee-" + i ,box).start();
        }

        new Bear("x1",box).start();
        new Bear("x2", box).start();

    }
}
//蜜蜂
class Bee extends Thread{
    private String name;
    private Box box;
    public Bee(String name,Box box){
        this.box = box;
        this.name = name;
    }
    public void run(){
        while (true){
            box.add();
            System.out.println(name + "生产了蜂蜜");
        }
    }
}
//熊
class Bear extends Thread{
    private String name;
    private Box box;
    public Bear(String name,Box box){
        this.box = box;
        this.name = name;
    }
    public void run(){
        while (true){
            box.remove();
            System.out.println(name + "吃掉了蜂蜜");
        }
    }


}
//蜜罐
class Box{
    private int MAX = 20;
    private int count;
    //添加蜂蜜 +1
    public synchronized void add(){
        while (count >= MAX){
            try{

                this.wait();
            }catch (Exception e){}
        }
        count ++ ;



    }
    //移除蜂蜜 - MAX
    public synchronized void remove(){
        while (count < MAX) {
            try {
                this.wait();
            }catch (Exception e){

            }
            count = 0;
            this.notify();
        }


    }

}

====================================================
java. lang.Runnable
Runnable接口
1.接口
2.public void run()
3.提供现有类实现线程功能
4.使用runnable对象创建线程
new Thread(Runnable r).start()
================================

class Car implements Runnable{
	...
}

new Thread(Runnable r ).start();

s.start();
s.run();

===============================================

创建线程方法二

**实现Runnable接口
1.自雷覆盖接口中的run方法
2.通过Thread类创建线程,并将实现了Runnable接口的子类对象作为参数传递给Thread类的构造函数
3.Thread类调用start方法开启线程
**
============================

Runnable和Thread的区别

在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口;Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限,

package org.thread.demo;

  class MyThread extends Thread{

  private String name;

  public MyThread(String name) {

  super();

  this.name = name;

  }

  public void run(){

  for(int i=0;i<10;i++){

  System.out.println("线程开始:"+this.name+",i="+i);

  }

  }

  }

  package org.thread.demo;

  public class ThreadDemo01 {

  public static void main(String[] args) {

  MyThread mt1=new MyThread("线程a");

  MyThread mt2=new MyThread("线程b");

  mt1.run();

  mt2.run();

  }

  }

在JDK的安装路径下,src.zip是全部的java源程序,通过此代码找到Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,那么这样的技术成为JNI技术

public interface Runnable{

  public void run();

  }

  例子:

  package org.runnable.demo;

  class MyThread implements Runnable{

  private String name;

  public MyThread(String name) {

  this.name = name;

  }

  public void run(){

  for(int i=0;i<100;i++){

  System.out.println("线程开始:"+this.name+",i="+i);

  }

  }

  };

=========================

线程状态

============================================
1.NEW

尚未运行

2.RUNNABLE

正在运行的状态

3.BLOCKED

//等待等待检视器的锁定权
synchronized(this)

4.WAITING

//等待状态(无线等待)
一个线程在等待另一个线程特定的操作

5.TIMED WAITING

//时限等待
//等待指定的时间

6.TERMINATED

//线程退出

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oifengo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值