Advertisement
jaVer404

level17.lesson04.task01

Sep 24th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. package com.javarush.test.level17.lesson04.task01;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* Синхронизированные заметки
  7. 1. Класс Note будет использоваться нитями.
  8.    Поэтому сделай так, чтобы обращения к листу notes блокировали мютекс notes, не this
  9. 2. Все System.out.println не должны быть заблокированы (синхронизированы),
  10.    т.е. не должны находиться в блоке synchronized
  11. */
  12.  
  13. public class Solution {
  14.     public static class Note {
  15.  
  16.         public final List<String> notes = new ArrayList<String>();
  17.  
  18.                         //addNote
  19. /*-----------------------------------------------------*/
  20.         public void addNote(int index, String note) {
  21.             System.out.println("Сейчас будет добавлена заметка [" + note + "] На позицию " + index);
  22.  
  23.             synchronized (notes) {
  24.             notes.add(index, note);
  25.             }
  26.  
  27.             System.out.println("Уже добавлена заметка [" + note + "]");
  28.         }
  29. /*-----------------------------------------------------*/
  30.  
  31.                        //removeNote
  32. /*-----------------------------------------------------*/
  33.         public void removeNote(int index) {
  34.             System.out.println("Сейчас будет удалена заметка с позиции " + index);
  35.             String note;
  36.             String temp;
  37.             synchronized (notes)
  38.             {
  39.                 temp = notes.remove(index);
  40.                 note = temp;
  41.             }
  42.  
  43.             System.out.println("Уже удалена заметка [" + note + "] с позиции " + index);
  44.         }
  45. /*-----------------------------------------------------*/
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement