遍历100以内的所有偶数——继承Thread类的方法
/**
* 多线程的创建, 方式一:继承Thread类
* 1,创建一个继承与Thread类的子类
* 2,重写Thread类的run()方法 -->将此线程执行的操作声明在run方法中
* 3,创建Thread类的子类对象
* 4,通过此对象调用start方法
*
* 例子:遍历100以内的所有偶数
*
* @Author zfj
* @create 2019/10/25 13:59
*/
class MyThread extends Thread{
public void run() {
for(int i=0;i<=100;i++){
System.out.println(i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
//问题二,再启动一个线程
MyThread t2 = new MyThread();
t2.start();
for(int i=0;i<=100;i++){
System.out.println(i+"*");
}
}
}
遍历100以内的所有偶数——实现runnable接口的方法
/**
* 创建多线程的方式二: 实现runnable接口
* 1,创建一个实现类runnable接口的类
* 2,实现类去实现runnable中的抽象方法:run()
* 3,创建实现类的对象
* 4,将此对象最为参数传递到Thread类的构造器中,创建Thread类的对象
* 5,通过Thread类的对象调用start()
*
*
* 比较两种创建线程的方式。
*
* 开发中:优先选择: 实现runnable接口的方式
* 原因:1,实现的方式没有类的单继承的局限性
* 2, 实现的方式更适合来处理来处理多个线程有共享数据的情况
* 联系:public class Thread impliments Runnable
* 相同点:两种方式都重写了run方法,将线程要执行的逻辑声明在run里面。
* @Author zfj
* @create 2019/10/26 13:26
*/
class MThread implements Runnable{
public void run() {
for(int i=0;i<100;i++){
if(i%2==0)
System.out.println(Thread.currentThread().getName()+":"+i+":");
}
}
}
public class ThreadTest1 {
public static void main(String[] args) {
MThread mThread=new MThread();
Thread t1 = new Thread(mThread);
Thread t2=new Thread(mThread);
t1.setName("线程一");
t2.setName("线程二");
t1.start();
t2.start();
}
}
创建两个分线程,其中一个遍历100以内的偶数,另一个遍历基数
/**
* 练习:创建两个分线程,其中一个遍历100以内的偶数,另一个遍历基数
*
* @Author zfj
* @create 2019/10/25 14:26
*/
public class ThreadDemo {
public static void main(String[] args) {
// MyThread1 t1 = new MyThread1();
// MyThread2 t2 = new MyThread2();
// t1.start();
// t2.start();
//创建Thread类的匿名子类的方式
new Thread(){
public void run() {
for(int i=0;i<100;i++){
if(i%2==0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}.start();
new Thread(){
public void run() {
for(int i=0;i<100;i++){
if(i%2!=0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}.start();
}
}
class MyThread1 extends Thread{
public void run() {
for(int i=0;i<100;i++){
if(i%2==0){
System.out.println(i);
}
}
}
}
class MyThread2 extends Thread{
public void run() {
for(int i=0;i<100;i++){
if(i%2!=0){
System.out.println(i+"*");
}
}
}
}
测试Thread类中的常用方法
/**
* 测试Thread类中的常用方法
* 1.start();启动当前线程,调用当前的run方法()
* 2.run();通常需要重写Thread类中的此方法,将创建线程的具体操作方法写在这里面
* 3.currentThread();静态方法,返回执行当前代码的线程
* 4.getName();过去当前线程的名字
* 5.setName();设置当前线程的方法
* 6.yield();释放当前cpu的执行权
* 7.join();在线程a中调用线程b的join方法,此时线程a就进入阻塞状态,直到线程b执行完毕之后,线程a才结束阻塞状态
* 8.stop();已过时
* 9.sleep(long millitime);让我们的线程睡眠,指定时间mililitime里
*
*
* 程序的优先级
* MAX_PRIORITY: 10
* MIN_PRIORITY: 1
* NORM_PRIORITY: 5
*
* 如何来获取和设置当前程序的优先级
* getPriority(): 获取当前线程的优先级
* setPriority(): 设置当前线程的优先级
*
*
* @Author zfj
* @create 2019/10/25 14:42
*/
public class ThreadMethodTest {
public static void main(String[] args) {
HelloThread h1 = new HelloThread("分线程:");
// h1.setName("线程一");
//设置分线程的优先级
h1.setPriority(Thread.MAX_PRIORITY);
h1.start();
//给主线程命名
Thread.currentThread().setName("主线程:");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
for(int i=0;i<100;i++){
if(i%2==0){
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":"+i+"*");
}
// if(i==20) {
// try {
// h1.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
System.out.println(h1.isAlive());
}
}
class HelloThread extends Thread{
public void run() {
for(int i=0;i<100;i++){
if(i%2==0){
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+i);
}
// if(i%20==0){
// this.yield();
// }
}
}
public HelloThread(String name){
super(name);
}
}
创建三个窗口卖票,总票数为100张 ,此时用的方法是方法一:继承Thread类的方法 (线程不安全)
/**
*
* 例子:创建三个窗口卖票,总票数为100张 ,此时用的方法是方法一:继承Thread类的方法
* @Author zfj
* @create 2019/10/26 13:13
*/
class Window extends Thread{
//此ticket必须定义成静态
private static int ticket = 100;
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket>0){
System.out.println(getName()+":卖票,票号为:"+ticket);
ticket--;
}else
break;
}
}
}
public class WindowTest {
public static void main(String[] args) {
Window t1=new Window();
Window t2=new Window();
Window t3=new Window();
t1.setName("窗口一");
t2.setName("窗口二");
t3.setName("窗口三");
t1.start();
t2.start();
t3.start();
}
}
创建三个窗口卖票,总票数为100张 使用runnable接口的方法 (线程不安全)
/**
* 例子:创建三个窗口卖票,总票数为100张 使用runnable接口的方法
*
* @Author zfj
* @create 2019/10/26 13:41
*/
class mWindow2 implements Runnable{
private int ticket=100;
public void run() {
while(true){
if(ticket>0){
System.out.println(Thread.currentThread().getName()+";票号:"+ticket);
ticket--;
}else
break;
}
}
}
public class WindowTest2 {
public static void main(String[] args) {
mWindow2 mw2=new mWindow2();
Thread t1=new Thread(mw2);
Thread t2=new Thread(mw2);
Thread t3=new Thread(mw2);
t1.setName("票台1");
t2.setName("票台2");
t3.setName("票台3");
t1.start();
t2.start();
t3.start();
}
}
创建三个窗口卖票,总票数为100张 使用runnable接口的方法 (1) (线程安全,同步代码块)
/**
* 例子:创建三个窗口卖票,总票数为100张 使用runnable接口的方法
*
*
* 1. 问题:卖票过程中,出现里错票,重票 --》出现里线程的安全问题
* 2. 问题出现的原因:当某个线程操作车票的过程中,尚未完成操作,其他线程参与进来,也操作车票
* 3. 如何解决:当一个线程在操作ticket时,其他线程不能参与进来。知道线程a操作完ticket时,其他线程才可以开始操作ticket
* 即使线程a出现里阻塞,那也要等线程a结束。
* 4. 在Java中,我们通过同步机制,来解决线程的安全问题。
*
* 方式一:同步代码块
*
* synchronized(同步监视器){
* //需要被同步的代码
* }
* 说明:1 .(什么叫需要被同步的代码) 操作共享数据的代码, --》包含的代码,不能多也不能少
* 2 .共享数据:多个线程共同操作的变量。比如:ticket 就是共享数据
* 3 .同步监视器,俗称:锁。任何一个类的对象,都可以来充当锁
* 要求:多个线程必须要公用同一把锁。
*
* 补充:在实现runnable接口创建多线程的方式中,我们可以考虑使用this来充当同步监视器(锁)
*
* 方式二:同步方法
*
* 如果操作共享数据的代码完整的声明在一个方法中,我们可以将此方法声明同步
*
* 5.同步的方式,解决了线程的安全问题。-------好处
* 操作同步代码时,只能由一个线程参与,其他线程等待。相当于一个单线程,效率低。--------坏处
*
* @Author zfj
* @create 2019/10/26 13:41
*/
class mWindow2 implements Runnable{
private int ticket=100;
Object obj=new Object();
public void run() {
while(true){
synchronized(obj){
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+";票号:"+ticket);
ticket--;
}else
break;
}
}
}
}
public class WindowTest2 {
public static void main(String[] args) {
mWindow2 mw2=new mWindow2();
Thread t1=new Thread(mw2);
Thread t2=new Thread(mw2);
Thread t3=new Thread(mw2);
t1.setName("票台1");
t2.setName("票台2");
t3.setName("票台3");
t1.start();
t2.start();
t3.start();
}
}
创建三个窗口卖票,总票数为100张 使用runnable接口的方法 (2) (线程安全,同步方法)
/**
* 使用同步方法解决runnable接口的线程安全问题
*
* 关于同步方法的总结:
* 1.同步方法任然涉及到同步监视器,只是不需要我们显示的声明。
* 2.非静态的同步方法,同步监视器是: this
* 静态的同步方法,同步监视器是:当前类本身
*
* @Author zfj
* @create 2019/10/27 13:50
*/
class mwT4 implements Runnable{
private int ticket=100;
public void run() {
while(true){
show();
}
}
private synchronized void show(){ //同步监视器:this
//synchronized(this){
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--->>票号:"+ticket);
ticket--;
}
//}
}
}
public class WindowTest4 {
public static void main(String[] args) {
mwT4 m4=new mwT4();
Thread t1=new Thread(m4);
Thread t2=new Thread(m4);
Thread t3=new Thread(m4);
t1.setName("票台一");
t2.setName("票台二");
t3.setName("票台三");
t1.start();
t2.start();
t3.start();
}
}
创建三个窗口卖票,总票数为100张 使用继承Thread类的方法 (线程安全,同步代码块)
/**
*
* 使用同步代码块解决Thread类的方式的线程安全问题
*
* 说明:在继承Thread类创建多线程的方式中,慎用this来充当锁的方式,一定要考虑此this是不是唯一
*
* @Author zfj
* @create 2019/10/26 18:43
*/
class mt3 extends Thread{
private static int ticket=100;
private static Object obj=new Object();
public void run() {
while(true){
synchronized(obj){
//或者synchronized(mt3.class){ 因为这个类,只会加载一次,所有也可以充当锁
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":----->>票号:"+ticket);
ticket--;
}else
break;
}
}
}
}
public class WindowTest3 {
public static void main(String[] args) {
mt3 mtt1 = new mt3();
mt3 mtt2 = new mt3();
mt3 mtt3 = new mt3();
mtt1.setName("票台一");
mtt2.setName("票台二");
mtt3.setName("票台三");
mtt1.start();
mtt2.start();
mtt3.start();
}
}
创建三个窗口卖票,总票数为100张 使用继承Thread类的方法 (线程安全,同步方法)
/**
*
* 使用同步方法来处理继承thread类的线程安全问题
*
* @Author zfj
* @create 2019/10/27 14:07
*/
class wmT5 extends Thread{
private static int ticket=100;
public void run() {
while(true){
show();
}
}
private static synchronized void show(){//现在的锁唯一了: 谁呢,是 wmT5
//private synchronized void show(){//此种线程解决方式是错误的, 原因为 锁,这里的锁不唯一,所以要改成静态的
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"----->>票号:"+ticket);
ticket--;
}
}
}
public class WindowTest5 {
public static void main(String[] args) {
wmT5 t1=new wmT5();
wmT5 t2=new wmT5();
wmT5 t3=new wmT5();
t1.setName("票台一");
t2.setName("票台二");
t3.setName("票台三");
t1.start();
t2.start();
t3.start();
}
}
银行有一个账户,有两个储户分别向同一个账户存3000元,每次存1000,存三次。每次存完打印账户余额。
(继承Thread类方法,线程安全)
/**
*
* 银行有一个账户
* 有两个储户分别向同一个账户存3000元,每次存1000,存三次。每次存完打印账户余额。
*
* 分析
* 1。是否是多线程? 是!,两个储户
* 2.是否有共享数据? 有,账户(或账户余额)
* 3.是否有线程安全问题? 有
* 4.需要考虑如何解决线程安全问题?同步机制:有三种方式。
*
* @Author zfj
* @create 2019/10/27 15:41
*/
class Account{
private double balance;
public Account(double balance) {
this.balance = balance;
}
//存钱
public synchronized void deposit(double amt){
if(amt>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
balance+=amt;
System.out.println(Thread.currentThread().getName()+"存钱成功。余额为:"+balance);
}
}
}
class Customer extends Thread{
private Account acct;
public Customer(Account acct) {//在构造器当中,对属性做一个实例化
this.acct = acct;
}
public void run() {
for(int i=1;i<4;i++){
acct.deposit(1000);
}
}
}
public class AccountTest {
public static void main(String[] args) {
Account acct=new Account(0);
Customer c1=new Customer(acct);
Customer c2=new Customer(acct); //这里实现了 两个线程处理同一个账户
c1.setName("甲");
c2.setName("乙");
c1.start();
c2.start();
}
}
(实现runnable接口的方法,线程安全)
/**
*
* 使用实现runnable的方式
*
* @Author zfj
* @create 2019/10/27 16:02
*/
class Account1{
private static double balance;
public Account1(double balance) {
this.balance = balance;
}
//存钱
public synchronized void desposit(double amt){
if(amt>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
balance+=amt;
System.out.println(Thread.currentThread().getName()+"存入:"+balance);
}
}
}
class Customer1 implements Runnable{
private Account1 acct;
public Customer1(Account1 acct) {
this.acct = acct;
}
public void run() {
for(int i=1;i<4;i++){
acct.desposit(1000);
}
}
}
public class AccountTest1 {
public static void main(String[] args) {
Account1 acct=new Account1(0);
Customer1 c1=new Customer1(acct);
Customer1 c2=new Customer1(acct);
Thread t1=new Thread(c1);
Thread t2=new Thread(c2);
t1.setName("甲");
t2.setName("乙");
t1.start();
t2.start();
}
}
使用同步机制将单例模式中的懒汉式写成线程安全的
/**
*
* 使用同步机制将单例模式中的懒汉式写成线程安全的
*
* @Author zfj
* @create 2019/10/27 14:19
*/
public class BankTest {
}
//懒汉式
class Bank{
private Bank(){}
private static Bank instance=null;
//用同步方法 的方式,加个synchronized关键字就行了
//private static synchronized Bank getInstance(){//此时 线程已经安全,其中的锁 是Bank.class ,(因为前面有static)
private static Bank getInstance(){
//方式一
// synchronized (Bank.class) { //此方法和上面的效果一样,但是!!!效率较差
// if(instance==null){
// instance=new Bank();
// }
// return instance;
// }
//方式二
if(instance==null){
synchronized (Bank.class){
if(instance==null){
instance=new Bank();
}
}
}
return instance;
}
}
解决线程安全问题的方式三: Lock锁
/**
*
* 解决线程安全问题的方式三: Lock锁--------JDK5.0新增
*
* 1.面试题:synchronized与Lock的异同?
* 同:二者都可以解决线程的安全问题
* 不同:synchronized机制在执行完相应的同步代码以后,自动的释放同步监视器
* Lock需要手动的启动同步,同事结束同步也需要手动实现
*
* 2.优先使用顺序:
* Lock----》同步代码块-----》同步方法
*
*
* @Author zfj
* @create 2019/10/27 15:21
*/
class Window implements Runnable{
private int ticket =100;
//1,实例化
private ReentrantLock lock=new ReentrantLock();
public void run() {
while(true){
try{
//2调用lock方法
lock.lock();
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---:票号:"+ticket);
ticket--;
}else
break;
}finally {
//3.调用解锁的方法:unlock
lock.unlock();
}
}
}
}
public class LockTest {
public static void main(String[] args) {
Window w=new Window();
Thread t1=new Thread(w);
Thread t2=new Thread(w);
Thread t3=new Thread(w);
t1.setName("票台一");
t2.setName("票台二");
t3.setName("票台三");
t1.start();
t2.start();
t3.start();
}
}
演示线程死锁问题(1)
/**
*
* 演示线程死锁问题
*
* 1,死锁的理解:不同的线程分别占用对方需要的同步资源不放弃,都在等待对方放弃自己需要的同步资源,就会形成死锁
*
* @Author zfj
* @create 2019/10/27 14:53
*/
public class ThreadTest {
public static void main(String[] args) {
StringBuffer s1=new StringBuffer();
StringBuffer s2=new StringBuffer();
new Thread(){
public void run() {
synchronized (s1){
s1.append("a");
s2.append("1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s2){
s1.append("b");
s2.append("2");
System.out.println(s1);
System.out.println(s2);
}
}
}
}.start();
new Thread(new Runnable() {
public void run() {
synchronized(s2){
s1.append("c");
s2.append("3");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s1){
s1.append("d");
s2.append("4");
System.out.println(s1);
System.out.println(s2);
}
}
}
}).start();
}
}
** 这里有两个资源,s1和s2,有两个线程,都是安全的线程。
线程一用s1当锁,线程二用s2当锁。
当线程一里面要用到s2资源时它只能等待,等待线程二结束释放s2。
但是!线程二也遇到了和线程一样的问题,它握着s2,需要用到s1,在等待线程一结束释放s1资源。
演示线程死锁问题(2)
/**
* @Author zfj
* @create 2019/10/27 15:08
*/
class A{
public synchronized void foo(B b){
System.out.println("当前线程名:"+ Thread.currentThread().getName()+"进入了A实例的foo方法");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("当前线程名:"+Thread.currentThread().getName()+"企图调用B实例的last方法");
b.last();
}
public synchronized void last(){
System.out.println("进入A类的last方法内部。");
}
}
class B{
public synchronized void foo(A a){
System.out.println("当前线程名:"+Thread.currentThread().getName()+"进入了B的实例的foo方法");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("当前线程名:"+Thread.currentThread().getName()+"企图调用A实例的last方法");
a.last();
}
public synchronized void last(){
System.out.println("进入B类的last方法内部");
}
}
public class DeadLock implements Runnable{
A a=new A();
B b=new B();
public static void main(String[] args) {
DeadLock dl=new DeadLock();
new Thread(dl).start();
dl.init();
}
public void init(){
Thread.currentThread().setName("主线程");
a.foo(b);
System.out.println("进入了主线程之后");
}
public void run() {
Thread.currentThread().setName("副线程");
b.foo(a);
System.out.println("进入了副线程之后");
}
}
线程通信
线程通信的例子:使用两个线程打印1-100 ,线程1,线程2 交替打印——runnable
/**
*
* 线程通信的例子:使用两个线程打印1-100 ,线程1,线程2 交替打印
*
* 涉及到的三个方法:
* wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器
* notify():一旦执行此方法,就会唤醒被wait的一个线程。如果有多个线程被wait,就唤醒优先级高的
* notifyAll():一旦执行此方法,就会唤醒所有被wait的线程
*
* 说明:
* 1.wait(),notify(),notifyAll()三个方法必须使用在同步代码块或者同步方法中。
* 2.这三个方法的调用者必须是。。。。。(要都是同一个锁)
*
* 面试题:sleep()和wait()的异同?
* 1.相同点:一旦执行,都可以让当前线程进入阻塞状态。
* 2.不同点:1)两个方法声明的位置不同:thread类中声明sleep(),Object类中声明wait()
* 2)调用的要求不同:sleep()可以在任何需要的场景下调用。wait()必须在同步代码块或同步方法中调用
* 3)关于是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep方法不会释放锁,wait会释放锁。
* @Author zfj
* @create 2019/10/27 16:16
*/
class Number implements Runnable{
private int number=1;
public void run() {
while(true){
synchronized (this) {//this:唯一的number对象
notify();
if(number<101){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+";"+number);
number++;
try {
//使得调用如下方法的线程进入阻塞状态
wait(); //执行wait时它会释放锁,这与sleep不同
} catch (InterruptedException e) {
e.printStackTrace();
}
}else
break;
}
}
}
}
public class CommunicationTest {
public static void main(String[] args) {
Number num=new Number();
Thread t1=new Thread(num);
Thread t2=new Thread(num);
t1.setName("线程一");
t2.setName("线程二");
t1.start();
t2.start();
}
}
线程通信的应用:经典问题:生产者/消费者问题
- 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员出取走产品,
- 店员一次只能持有固定数量的产品,如果生产者试图产生更多的产品,店员会叫生产者停一下,
- 如果店中有空位放产品了再通知生产者继续生产
- 如果店中没有产品了,店员会告诉消费者等一下,
- 如果店中有了产品再通知消费者来取走产品
分析:
-
1,是否是多线程? 是,生产者线程,消费者线程
-
2,是否有共享数据? 有,店员(或者产品,产品数量)
-
3,如何解决线程安全问题?同步机制,有三种方法
-
4,是否涉及到线程通信?是
(Thread)
class Clerk{
private int productCount=0;
//生产产品
public synchronized void produceProduct() {
if(productCount<20){
productCount++;
System.out.println(Thread.currentThread().getName()+":开始生产第"+productCount+"个产品");
notify();
}else{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//消费产品
public synchronized void customeProduct(){
if(productCount>0){
System.out.println(Thread.currentThread().getName()+":开始消费第"+productCount+"个产品");
productCount--;
notify();
}else{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer extends Thread{
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(Thread.currentThread().getName()+"开始生产产品。。。。");
while(true){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk.produceProduct();
}
}
}
class Customer extends Thread{
private Clerk clerk;
public Customer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(Thread.currentThread().getName()+"开始消费产品。。。。");
while(true){
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk.customeProduct();
}
}
}
public class ProductTest {
public static void main(String[] args) {
Clerk clerk=new Clerk();
Producer p1=new Producer(clerk);
p1.setName("生产者");
Customer c1=new Customer(clerk);
c1.setName("消费者");
p1.start();
c1.start();
}
}
(runnable)
/**
* @Author zfj
* @create 2019/10/27 17:28
*/
class Clerk1{
private int num=0;
public synchronized void producerProduc() {
if(num>20){
num++;
System.out.println(Thread.currentThread().getName()+"开始生产第"+num+"个产品");
}else{
notify();
}
}
public synchronized void customerProduct() {
if(num>0){
System.out.println(Thread.currentThread().getName()+"开始消费第"+num+"个产品");
num--;
}else{
notify();
}
}
}
class Producter1 implements Runnable{
private Clerk1 clerk1;
public Producter1(Clerk1 clerk1) {
this.clerk1 = clerk1;
}
public void run() {
while(true){
System.out.println(Thread.currentThread().getName()+"开始生产产品。。。");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk1.producerProduc();
}
}
}
class Customer1 implements Runnable{
private Clerk1 clerk1;
public Customer1(Clerk1 clerk1) {
this.clerk1 = clerk1;
}
public void run() {
while(true){
System.out.println(Thread.currentThread().getName()+"开始消费产品。。。");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk1.customerProduct();
}
}
}
public class ProductTest1 {
public static void main(String[] args) {
Clerk1 clerk1=new Clerk1();
Customer1 c1=new Customer1(clerk1);
Producter1 p1=new Producter1(clerk1);
}
}