java多线程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class ThreadSx {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread01 thread01 = new Thread01();
            thread01.start();
        }
        Thread02 thread02 = new Thread02();
        for (int i = 0; i < 10; i++) {
          new Thread(thread02).start();
        }
        for (int i = 0; i < 10; i++) {
            FutureTask<String> ft = new FutureTask<>(new Thread03());
            new Thread(ft).start();
            System.out.println(ft.get());
        }
    }
}


class Thread01 extends Thread{
    public void run() {
        System.out.println("thread实现=>"+Thread.currentThread().getName());
    }

}

class Thread02 implements Runnable{

    @Override
    public void run() {
        System.out.println("runnable实现=>"+Thread.currentThread().getName());
    }
}

class Thread03 implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "callable实现=>"+Thread.currentThread().getName();
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
yeield:在这里插入图片描述

public class YieldTest {
    public static void main(String[] args) {
        ThreadYield threadYield = new ThreadYield();
        new Thread(threadYield,"a").start();
        new Thread(threadYield,"b").start();
    }
}
class ThreadYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"===>开始执行");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"===>执行结束");
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class DeamonTest {
    public static void main(String[] args) {
        God god = new God();
        People people = new People();

        new Thread(people,"people").start();

        Thread godThread = new Thread(god,"god");
        godThread.setDaemon(true);
        godThread.start();

    }
}
class God implements Runnable{

    @Override
    public void run() {
        while(true){
            System.out.println(Thread.currentThread().getName()+"==>守护着你");
        }

    }
}

class People implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 30000; i++) {
            System.out.println(Thread.currentThread().getName()+"==>活着第"+i+"天");
        }
    }
}

在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

public class UnScychronizedTest01 {
    public static void main(String[] args) {
        BuyTicker buyTicker= new BuyTicker();
            new Thread(buyTicker,"小明").start();
            new Thread(buyTicker,"大明").start();
            new Thread(buyTicker,"黄牛党").start();
    }
}

class BuyTicker implements Runnable{

    private int tickerName=10;

    private boolean flag = true;
    @Override
    public void run() {
        while(flag){
            buy();
        }

    }

    private void buy(){
        if(tickerName<=0){
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"===>拿到了"+tickerName--);
    }
}
package com.thread.demo01;

public class UnScychronizedTest02 {
    public static void main(String[] args) {
        Account account = new Account(100,"结婚基金");
        Drawing you = new Drawing(account,20,"你");
        Drawing  grilFriend = new Drawing(account,100,"grilFriend");
        you.start();
        grilFriend.start();
    }
}
class Account{
    int money;
    String name;

    public Account(int money,String name){
        this.money = money;
        this.name = name;
    }
    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Drawing extends Thread{
    Account account;
    int drawMoney;
    int nowMoney;
    public Drawing( Account account,int drawMoney,String name){
        super(name);
        this.account = account;
        this.drawMoney = drawMoney;
    }

    @Override
    public void run() {
        if (account.money - drawMoney < 0 ){
            System.out.println(Thread.currentThread().getName()+"钱不够了,取不了");
            return;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        account.money = account.getMoney()-drawMoney;
        nowMoney = nowMoney+drawMoney;
        System.out.println(account.getName()+"余额为:"+account.getMoney());
        System.out.println(this.getName()+"手里的钱:"+nowMoney);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

public class UnScychronizedTest01 {
    public static void main(String[] args) {
        BuyTicker buyTicker= new BuyTicker();
            new Thread(buyTicker,"小明").start();
            new Thread(buyTicker,"大明").start();
            new Thread(buyTicker,"黄牛党").start();
    }
}

class BuyTicker implements Runnable{

    private int tickerName=10;

    private boolean flag = true;
    @Override
    public void run() {
        while(flag){
            buy();
        }

    }

    private synchronized void buy(){
        if(tickerName<=0){
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"===>拿到了"+tickerName--);
    }
}

在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

public class UnScychronizedTest02 {
    public static void main(String[] args) {
        Account account = new Account(100,"结婚基金");
        Drawing you = new Drawing(account,20,"你");
        Drawing  grilFriend = new Drawing(account,100,"grilFriend");
        you.start();
        grilFriend.start();
    }
}
class Account{
    int money;
    String name;

    public Account(int money,String name){
        this.money = money;
        this.name = name;
    }
    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Drawing extends Thread{
    Account account;
    int drawMoney;
    int nowMoney;
    public Drawing( Account account,int drawMoney,String name){
        super(name);
        this.account = account;
        this.drawMoney = drawMoney;
    }

    @Override
    public  void run() {
        synchronized (account){
            if (account.money - drawMoney < 0 ){
                System.out.println(Thread.currentThread().getName()+"钱不够了,取不了");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money = account.getMoney()-drawMoney;
            nowMoney = nowMoney+drawMoney;
            System.out.println(account.getName()+"余额为:"+account.getMoney());
            System.out.println(this.getName()+"手里的钱:"+nowMoney);
        }
        }
}

在这里插入图片描述

package com.thread.demo01;

public class DeadLockTest {
    public static void main(String[] args) {
        MakeUp g1 = new MakeUp(0,"灰姑凉");
        MakeUp g2 = new MakeUp(1,"白雪公主");
        g1.start();
        g2.start();
    }
}
//口红
class Lipstick{

}

//镜子
class Mirror{

}

class MakeUp extends Thread{
    static Lipstick lipstick = new Lipstick();
    static  Mirror mirror = new Mirror();

    int choice;//选择
    String grilName;//使用化妆品的人
    MakeUp(int choice,String grilName){
        this.choice = choice;
        this.grilName = grilName;
    }

    @Override
    public void run() {
        try {
            makeUp();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void makeUp () throws InterruptedException {
        if(choice ==0){
            //获得口红
            synchronized (lipstick){
                System.out.println(this.grilName + "获得口红");

                Thread.sleep(1000);
                //获得镜子
                synchronized (mirror){
                    System.out.println(this.grilName + "获得镜子");
                }
            }
        }else {
            //获得镜子
            synchronized (mirror){
                System.out.println(this.grilName + "获得镜子");
                Thread.sleep(1000);
                //获得镜子
                synchronized (lipstick){
                    System.out.println(this.grilName + "获得口红");
                }
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    public static void main(String[] args) {
        BuyTicker01 buyTicker= new BuyTicker01();
        new Thread(buyTicker,"小明").start();
        new Thread(buyTicker,"大明").start();
        new Thread(buyTicker,"黄牛党").start();
    }
}

class BuyTicker01 implements Runnable{

    private int tickerName=10;

    private boolean flag = true;

    private Lock lock = new ReentrantLock();
    @Override
    public void run() {

            while(flag){
                lock.lock();
                try {
                    buy();
                }finally {
                    lock.unlock();
                }
            }



    }

    private  void buy(){
        if(tickerName<=0){
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"===>拿到了"+tickerName--);
    }
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

public class PcTest {
    public static void main(String[] args) {
        SynContainer synContainer = new SynContainer();
         new Product(synContainer).start();
         new Consumer(synContainer).start();

    }

}
//生产者
class Product extends Thread{
    SynContainer synContainer;
    Product(SynContainer synContainer){
        this.synContainer =synContainer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synContainer.push(new Chicken(i));
            System.out.println("生产了第"+i+"只鸡");
        }
    }
}
//消费者
class Consumer extends  Thread{
    SynContainer synContainer;
    Consumer(SynContainer synContainer){
        this.synContainer =synContainer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了第"+synContainer.pop().id+"只鸡");
        }
    }
}
//产品
class Chicken{
    int id; //产品编号

    public Chicken(int id) {
        this.id = id;
    }
}

//缓冲区
class SynContainer{
    //容器
    Chicken[] chickens = new Chicken[10];
    //容器计数器
    int count =0;
    //生产者放入产品
    public synchronized void push(Chicken chicken){
        // 容器满了,就需要等待消费者消费
        if(chickens.length == count){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //容器没有满,生产者生茶
        chickens[count] = chicken;
        count++;
        //可以通知消费者消费
        this.notifyAll();

    }

    public synchronized Chicken pop(){
        //判断是否可以消费
        if(0 == count){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 可以消费
        count--;
        this.notifyAll();
        return chickens[count];
    }
}

在这里插入图片描述

package com.thread.demo01;

public class PcTest02 {
    public static void main(String[] args) {
        Tv tv = new Tv();
        new Player(tv).start();
        new Watcher(tv).start();
    }
}

//生产者 演员
class Player extends Thread{
    Tv tv;
    Player(Tv tv){
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if(i%2==0){
                this.tv.play("西游记");
            }else {
                this.tv.play("三国演义");
            }
        }
    }
}
// 消费者 观众
class Watcher extends Thread{
    Tv tv;
    Watcher(Tv tv){
        this.tv = tv;
    }
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
           this.tv.watch();
        }
    }
}
// 产品 节目
class Tv{
    String voice; //表演节目
    boolean flag = true;

    //表演
    public synchronized void  play(String voice){
        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("演员表演了:"+voice);
        this.notifyAll();
        this.voice = voice;
        this.flag = !this.flag;
    }
    //观看
    public synchronized void watch(){
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("观众观看了:"+this.voice);
        this.notifyAll();
        this.flag = true;
    }
}

在这里插入图片描述
在这里插入图片描述

package com.thread.demo01;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PoolTest {

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.shutdown();
    }
}
class MyThread implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值