java:线程和进程

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

public class MyThread extends Thread {
    public MyThread() {}
    public MyThread(String name){
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(getName()+":"+i);
        }
    }
}
public class MyThreadDemo {
    public static void main(String[] args) {
        MyThread myThread1 = new MyThread();
        MyThread myThread2 = new MyThread();
        myThread1.start();
        myThread2.start();
        MyThread myThread3 = new MyThread("线程1");
        myThread3.start();
        System.out.println(Thread.currentThread().getName());
    }
}

在这里插入图片描述

public class ThreadPriority extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println(getName() + "-" + i);
        }
    }
}
public class ThreadPriorityDemo {
    public static void main(String[] args) {
        ThreadPriority threadPriority1 = new ThreadPriority();
        ThreadPriority threadPriority = new ThreadPriority();
        threadPriority.setName("线程3");
        threadPriority1.setName("线程1");
        System.out.println(threadPriority.getName()+"优先级:"+threadPriority.getPriority());
        System.out.println(threadPriority1.getName()+"优先级:"+threadPriority1.getPriority());
        System.out.println(Thread.MAX_PRIORITY);
        System.out.println(Thread.MIN_PRIORITY);
        System.out.println(Thread.NORM_PRIORITY);
        threadPriority.setPriority(1);
        threadPriority1.setPriority(3);
        threadPriority.start();
        threadPriority1.start();
    }
}

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

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
}
public class MyRunnableDemo {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
//        Thread thread = new Thread(myRunnable);
//        Thread thread1 = new Thread(myRunnable);
        Thread thread = new Thread(myRunnable, "张三");
        Thread thread1 = new Thread(myRunnable, "李四");
        thread.start();
        thread1.start();
    }
}

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

public class SellTicket implements Runnable {
    private int tickets = 100;
    @Override
    public void run() {
    	//死循环,一直处于卖票状态
        while (true) {
            if (tickets > 0) {
                System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
                tickets--;
            }
        }
    }
}
public class SellTicketDemo {
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

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

public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (obj){
                if (tickets > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
                    tickets--;
                }
            }
        }
    }
}
public class SellTicketDemo {
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

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

public class SellTicket implements Runnable {
    private int tickets = 100;
    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (tickets > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
                    tickets--;
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
public class SellTicketDemo {
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

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

public class Box {
    private int milk;
    private boolean state = false;
    public synchronized void put(int milk) {
    	//如果有牛奶,就是当前线程等待
        if(state){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没有牛奶
        this.milk = milk;
        System.out.println("送奶工放入第" + this.milk + "瓶奶");
        state = true;
        notifyAll();
    }
    public synchronized void get() {
    	//如果没有牛奶,使当前线程等待
        if(!state){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果有牛奶
        System.out.println("拿到第" + this.milk + "瓶奶");
        state = false;
        notifyAll();
    }
}
public class Producer implements Runnable {
    private Box b;
    public Producer(Box box) {
        this.b = box;
    }
    @Override
    public void run() {
        for (int i = 1; i < 5; i++) {
            b.put(i);
        }
    }
}
public class Customer implements Runnable {
    private Box b;
    public Customer(Box box) {
        this.b = box;
    }
    @Override
    public void run() {
        while(true){
            b.get();
        }
    }
}
public class BoxDemo {
    public static void main(String[] args) {
        Box b = new Box();
        Producer producer = new Producer(b);
        Customer customer = new Customer(b);
        Thread thread1 = new Thread(producer);
        Thread thread2 = new Thread(customer);
        thread1.start();
        thread2.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会编程的小企鹅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值