|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +/** |
| 24 | + * Guarded-suspension is a concurrent design pattern for handling situation when to execute some action we need |
| 25 | + * condition to be satisfied. |
| 26 | + * <p> |
| 27 | + * Implementation is based on GuardedQueue, which has two methods: get and put, |
| 28 | + * the condition is that we cannot get from empty queue so when thread attempt |
| 29 | + * to break the condition we invoke Object's wait method on him and when other thread put an element |
| 30 | + * to the queue he notify the waiting one that now he can get from queue. |
| 31 | + */ |
| 32 | +package com.iluwatar.guarded.suspension; |
| 33 | + |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +/** |
| 39 | + * Created by robertt240 on 1/26/17. |
| 40 | + */ |
| 41 | +public class App { |
| 42 | + /** |
| 43 | + * Example pattern execution |
| 44 | + * |
| 45 | + * @param args - command line args |
| 46 | + */ |
| 47 | + public static void main(String[] args) { |
| 48 | + GuardedQueue guardedQueue = new GuardedQueue(); |
| 49 | + ExecutorService executorService = Executors.newFixedThreadPool(3); |
| 50 | + |
| 51 | + //here we create first thread which is supposed to get from guardedQueue |
| 52 | + executorService.execute(() -> { |
| 53 | + guardedQueue.get(); |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + //here we wait two seconds to show that the thread which is trying to get from guardedQueue will be waiting |
| 58 | + try { |
| 59 | + Thread.sleep(2000); |
| 60 | + } catch (InterruptedException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + //now we execute second thread which will put number to guardedQueue and notify first thread that it could get |
| 64 | + executorService.execute(() -> { |
| 65 | + guardedQueue.put(20); |
| 66 | + } |
| 67 | + ); |
| 68 | + executorService.shutdown(); |
| 69 | + try { |
| 70 | + executorService.awaitTermination(30, TimeUnit.SECONDS); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments