interview_code

循环队列

package other;

/**
 * Created by fangjiejie on 2020/4/3.
 * 循环队列:使用具有一定容量的数组装载元素时,避免“假溢出”现象:已经达到数组尾部,不可再插入新的元素,会因数组越界出错
 * 而实际可用空间还没使用完。解决方案 是将顺序队列从逻辑上堪称一个环,成为循环队列,循环队列的首尾相接
 * <p>
 * 注意len的计算方式
 */
public class CycleQueue {
    int size = 0;
    int array[] = null;
    int front = 0;
    int tail = 0;

    public CycleQueue(int size) {
        this.size = size + 1;
        array = new int[this.size];
        front = 0;
        tail = 0;
    }

    public boolean add(int elem) {//入队
        if (isFull()) return false;
        array[tail] = elem;
        tail = (tail + 1) % size;
        return true;
    }

    public Integer poll() {//出队
        if (isNull()) return null;
        int elem = array[front];
        front = (front + 1) % size;
        return elem;
    }

    public int len() {//获取队列中元素数量
        return (tail + size - front) % size;
    }

    public void clean() {
        for (int i = 0; i < size; i++) {
            array[i] = 0;
        }
        this.size = 0;
        this.front = 0;
        this.tail = 0;
    }

    public boolean isFull() {
        if ((tail + 1) % size == front) return true;
        return false;
    }

    public boolean isNull() {
        if (front == tail) return true;
        return false;
    }

    public static void main(String[] args) {
        CycleQueue queue = new CycleQueue(5);
        for (int i = 1; i < 5; i++) {
            queue.add(i);
        }

        System.out.println(queue.len());
        System.out.println(queue.poll());
        System.out.println(queue.len());
        System.out.println(queue.add(8));
        System.out.println(queue.add(9));
        System.out.println(queue.add(10));
        System.out.println(queue.len());
        while (!queue.isNull()) {
            System.out.print(queue.poll() + "\t");
        }
    }
}

生产者消费者

package other;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by fangjiejie on 2020/3/15.
 */
//注意是put和take方法
public class Produ_Consum {
    public  static BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>(5);
    public static class  Producer extends Thread{
        @Override
        public void run() {
            super.run();
            try{
                blockingQueue.put(6);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("producer");
        }
    }
    public static class Consumer extends Thread{
        @Override
        public void run() {
            super.run();
            try{
                blockingQueue.take();
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("consumer");
        }

    }
    public static void main(String[] args) {
        for(int i=0;i<5;i++){
            Producer producer=new Producer();
            producer.start();
        }
       for(int i=0;i<15;i++){
           Consumer consumer=new Consumer();
           consumer.start();
       }
        for(int i=0;i<5;i++){
            Producer producer=new Producer();
            producer.start();
        }

    }
}

三个线程轮流打印1~1000

package other;

/**
 * Created by fangjiejie on 2020/3/15.
 */

class MyThread extends Thread {
    int id;
    public static int num = 0;
    public static Object lock = new Object();
    public MyThread(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (num > 1000) break;
                if (num % 3 == this.id) {
                    System.out.println(this.id + "------" + num);
                    num++;
                }
//                /* 可省略
                else{
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notifyAll();
//                */
            }
        }
    }

    public static void main(String[] args) {
        MyThread thread1 = new MyThread(0);
        MyThread thread2 = new MyThread(1);
        MyThread thread3 = new MyThread(2);
        thread1.start();
        thread2.start();
        thread3.start();
    }

}
package other.review;/**
 * Created by fangjiejie on 2020/9/2.
 */

/**
 * @author : liyafang
 * @description :
 * @date : 2020-09-02 17:18
 */
public class Mythread4 extends Thread{
    int id;
    static volatile int count;
    public Mythread4(int id){
        this.id = id;
    }
    public void run(){
        while (true){
            if(count > 100){
                break;
            }
            if(count % 3 == id){
                System.out.println(id + "--------" + count);
                count++;
            }
        }
    }

    public static void main(String[] args) {
        Mythread4 myThread1 = new Mythread4(0);
        Mythread4 myThread2 = new Mythread4(1);
        Mythread4 myThread3 = new Mythread4(2);
        myThread1.start();
        myThread2.start();
        myThread3.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值