java-线程相关

线程通信-生产者消费问题
package adwad153;

/**
 * 生产者和消费者问题
 */
public class dwad15 {
    public static void main(Stringargs) {
        Clerk clerk = new Clerk();
        Producter p1 = new Producter(clerk);
        p1.setName("生产者1");
        Consumer c1 = new Consumer(clerk);
        c1.setName("消费者1");
        Consumer c2 = new Consumer(clerk);
        c2.setName("消费者2");
        p1.start();
        c1.start();
        c2.start();
    }
}

class Clerk {
    private int count = 0;

    //生产
    public synchronized void product() {
        if (count < 20) {
            ++count;
            System.out.println(Thread.currentThread().getName() + ":开始生产第" + count + "个产品");
            notify();
        } else {
            //d等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //消费
    public synchronized void order() {
        if (count > 0) {
            System.out.println(Thread.currentThread().getName() + ":开始消费第" + count + "个产品");
            count--;
            notify();
        } else {
            //d等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Producter extends Thread {
    private Clerk clerk;

    public Producter(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println(this.getName() + ":开始生产产品");
        while (true) {
            try {
                this.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.product();
        }
    }
}

class Consumer extends Thread {
    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println(this.getName() + ":开始消费产品");
        while (true) {
            try {
                this.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.order();
        }
    }
}




线程池
package acxwacawecew;

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

class numberThread1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

class numberThread2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

public class threadpool {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service = (ThreadPoolExecutor) executorService;
        //设置线程池的属性
        service.setCorePoolSize(15);
        executorService.execute(new numberThread1());//适合于runnable
        executorService.execute(new numberThread2());//适合于runnable
//        executorService.submit();//适合于callable
        executorService.shutdown();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值