1. Thread Message using BlockingQueue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Queue { static BlockingQueue queue; public static void main(String[] args) { queue = new LinkedBlockingQueue(); new Thread( () -> { for( int i=0; i < 10; i++ ) { try { queue.put( "Message: " + i); Thread.sleep(100); } catch (InterruptedException e) { } } }).start(); while(true) { try { System.out.println(queue.take()); } catch (InterruptedException e) { } } } } |
Result:
Message: 0 Message: 1 Message: 2 Message: 3 Message: 4 Message: 5 Message: 6 Message: 7 Message: 8 Message: 9
댓글 없음:
댓글 쓰기