Advertisement
jaVer404

level17.lesson02.task01

Sep 24th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package com.javarush.test.level17.lesson02.task01;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* Заметки
  7. 1. Класс Note будет использоваться нитями.
  8.  
  9. 2. Создай public static нить NoteThread (Runnable не является нитью),
  10.     которая
  11.     в методе run 1000 раз (index = 0-999) сделает следующие действия:
  12.  
  13.     2.1. используя метод addNote добавит заметку с именем [getName() + "-Note" + index], например, при index=4
  14. "Thread-0-Note4"
  15.  
  16.  
  17. 2.2. используя метод removeNote удалит заметку
  18.  
  19. 2.3. в качестве первого параметра в removeNote передай имя нити - метод getName()
  20. */
  21.  
  22. public class Solution {
  23.     public static void main(String[] args) {
  24.        
  25.     }
  26.  
  27. /*-----------------------------------------------------------*/
  28.     public static class Note {
  29.  
  30.         public static final List<String> notes = new ArrayList<String>();
  31.  
  32.         public static void addNote(String note) {
  33.             notes.add(0, note);
  34.         }
  35.  
  36.         public static void removeNote(String threadName) {
  37.             String note = notes.remove(0);
  38.             if (note == null) {
  39.                 System.out.println("Другая нить удалила нашу заметку");
  40.             } else if (!note.startsWith(threadName)) {
  41.                 System.out.println("Нить [" + threadName + "] удалила чужую заметку [" + note + "]");
  42.             }
  43.         }
  44.     }
  45. /*---------------------------------------------------------*/
  46.  
  47.  
  48.     public static class NoteThread extends Thread {
  49.         @Override
  50.         public void run()
  51.         {
  52.             String someString;
  53.             for (int i = 0; i < 1000; i++) {
  54.                 someString = Thread.currentThread().getName() + "-Note" + i;
  55.                 Note.addNote(someString);
  56.                 Note.removeNote(Thread.currentThread().getName());
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement