Hi!
AFter running my program it shows - "Time out. The program ran too long and was closed."
But it seems that I did all conitions. so what's wrong?
thanks in advance!
package com.codegym.task.task17.task1701;
import java.util.ArrayList;
import java.util.List;
/*
Notes
*/
public class Solution {
public static void main(String[] args) {
new NoteThread().start();
new NoteThread().start();
}
public static class Note {
public static final List<String> notes = new ArrayList<>();
public static void addNote(String note) {
notes.add(0, note);
}
public static void removeNote(String threadName) {
String note = notes.remove(0);
if (note == null) {
System.out.println("Another thread deleted our note");
} else if (!note.startsWith(threadName)) {
System.out.println("Thread [" + threadName + "] deleted someone else's note [" + note + "]");
} else {
System.out.println("Thread [" + threadName + "] deleted its own note [" + note + "]");
}
}
}
public static class NoteThread extends Thread {
public void run() {
String name_of_note;
for (int i = 0; i < 1000; i++) {
name_of_note = getName() + "-Note" + i;
Note.addNote(name_of_note);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Note.removeNote(getName());
}
}
}
}
//1. The Note class will be used by threads.
//2. Create a public static NoteThread thread (the Runnable interface does not make a class a thread), whose run method does the following 1000 times (index = 0-999):
//2.1. Using the addNote method, add a note named [getName() + "-Note" + index]. For example, for index = 4:
//"Thread-0-Note4"
//2.2. Sleep for 1 millisecond
//2.3. Using the removeNote method, delete the note
//2.4. Pass the thread name (getName()) as the argument to removeNote
//Using the addNote method, add a note named [getName() + "-Note" + index]. For example, for index = 4:
//"Thread-0-Note4"
//2.2. Sleep for 1 millisecond
//2.3. Using the removeNote method, delete the note
//2.4. Pass the thread name (getName()) as the argument to removeNote