反转排序、存储,模拟Queue编写

xiaoxiao2021-02-28  73

1. 求从10到100中能被3或5整除的数的和

int sum = 0; for(int i = 10; i <= 100; i++) if( i % 3 == 0 || i % 5 == 0) sum += i; System.out.println(sum);

2. 将一个字符串逆序,不要使用反转函数

String message = "he saw a racecar"; StringBuilder rev = new StringBuilder(); for (int i = message.length()-1; i >= 0; i--) rev.append(message.charAt(i)); System.out.println(rev.toString());

3. 反转一个栈

Stack<Object> stack=new Stack<Object>() ; stack.push("he"); //stack last //he is at the bottom of the stack stack.push("saw"); stack.push("a"); stack.push("rececar");//stack top last-in-first-out Queue<Object> queue=new LinkedList<Object>(); //FIFO (first-in-first-out) while(stack.size()>0) queue.offer(stack.pop()); //top of remove and return value rececar a saw he while(queue.size()>0) stack.push(queue.poll()); //Retrieves and removes the head of this queue rececar a saw he while(stack.size()>0) System.out.println(stack.pop()); //he saw a rececar

使用wait/notify模拟Queue

BlockingQueue:顾名思义阻塞队列,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据。我们要实现LinkedBlockingQueue下面两个简单的方法put和get和take。 put(aObject):把一个对象aobject加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断,直到BlockingQueue里面有空间再继续。 take:取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态,直到BlockingQueue有新的数据被加入。 示例代码如下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 import java.util.LinkedList; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** * 使用wait/notify模拟Queue * */ public class ImitateQueue { // 承载元素的集合 private final LinkedList<Object> list = new LinkedList<Object>(); // 计数器进行计数 private final AtomicInteger count = new AtomicInteger( 0); // 制定元素的上限和下限 private final int maxSize; private final int minSize = 0; // 初始化一个对象用于加锁 private final Object lock = new Object(); public ImitateQueue(int maxSize) { this.maxSize = maxSize; } // 把一个对象aobject加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断,直到BlockingQueue里面有空间再继续增加 public void put(Object obj) { synchronized (lock) { while (count.get() == maxSize) { try { // 当队列中数据塞满,线程等待 lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } list.add(obj); // 计数器自增 count.getAndIncrement(); System.out.println( " 元素 " + obj + " 被添加 "); // 唤醒之前等待阻塞的take方法线程取数据 lock.notify(); } } // 取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态,直到BlockingQueue有新的数据被加入 public Object take() { Object temp = null; synchronized (lock) { while (count.get() == minSize) { try { // 当队列中的元素,取完,该线程等待 lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 计数器递减 count.getAndDecrement(); // 取出元素 temp = list.removeFirst(); System.out.println( " 元素 " + temp + " 被消费 "); // 唤醒之前阻塞的put方法把元素放进去 lock.notify(); } return temp; } public int size() { return count.get(); } public static void main(String[] args) throws Exception { final ImitateQueue m = new ImitateQueue( 5); m.put( "a"); m.put( "b"); m.put( "c"); m.put( "d"); m.put( "e"); System.out.println( "当前元素个数:" + m.size()); Thread t1 = new Thread( new Runnable() { @Override public void run() { m.put( "h"); m.put( "i"); } }, "t1"); t1.start(); Thread t2 = new Thread( new Runnable() { @Override public void run() { try { Object t1 = m.take(); System.out.println( "被取走的元素为:" + t1); Thread.sleep( 1000); Object t2 = m.take(); System.out.println( "被取走的元素为:" + t2); } catch (InterruptedException e) { e.printStackTrace(); } } }, "t2"); // 休眠2秒钟 TimeUnit.SECONDS.sleep( 2); t2.start(); } }

程序运行结果如下

1 2 3 4 5 6 7 8 9 10 11 12 元素 a 被添加 元素 b 被添加 元素 c 被添加 元素 d 被添加 元素 e 被添加 当前元素个数:5 元素 a 被消费 被取走的元素为:a 元素 h 被添加 元素 b 被消费 被取走的元素为:b 元素 i 被添加

转载请注明原文地址: https://www.6miu.com/read-67559.html

最新回复(0)