Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 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. 2. Создай public static нить NoteThread, которая в методе run 1000 раз (index = 0-999) сделает следующие
  9. действия:
  10. 2.1. используя метод addNote добавит заметку с именем [getName() + "-Note" + index], например, при index=4
  11. "Thread-0-Note4"
  12. 2.2. используя метод removeNote удалит заметку
  13. 2.3. в качестве первого параметра в removeNote передай имя нити - метод getName()
  14. */
  15.  
  16. public class Solution {
  17. public static void main(String[] args) {
  18. NoteThread thread=new NoteThread();
  19. thread.start();
  20. }
  21.  
  22. public static class Note {
  23.  
  24. public static final List<String> notes = new ArrayList<String>();
  25.  
  26. public static void addNote(String note) {
  27. notes.add(0, note);
  28. }
  29.  
  30. public static void removeNote(String threadName) {
  31. String note = notes.remove(0);
  32. if (note == null) {
  33. System.out.println("Другая нить удалила нашу заметку");
  34. } else if (!note.startsWith(threadName)) {
  35. System.out.println("Нить [" + threadName + "] удалила чужую заметку [" + note + "]");
  36. }
  37. }
  38. }
  39. public static class NoteThread extends Thread{
  40. @Override
  41. public void run() {
  42. for (int index = 0; index < 1000; index++) {
  43. Note.addNote(getName() + "-Note" + index);
  44. }
  45. Note.removeNote(getName());
  46. }
  47.  
  48.  
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement