Прошу прощения за небольшой поздний ответ, но вы можете попробовать это
public class Threads {
/**
* @param args
*/
public static void main(String[] args) {
//Thread Th = new Threads();
Thread th = new Thread (new thread1 ());
th.start();
Thread th1 = new Thread (new thread1 ());
th1.start();
}
}
class ThreadSample implements Runnable {
String name = "vimal";
static int id = 0;
public void run() {
System.out.println("Runnable " + this.name);
// setNAme("Manish");
synchronized (this) {
System.out.println(this.name);
this.name = "Manish " + this.id;
this.id++;
try {
wait();
System.out
.println("Thread " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void setName(String name) {
try {
System.out.println("Thread " + Thread.currentThread().getName());
wait(1000);
this.name = name;
System.out.println("Name " + this.name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Я действительно не знаю, как вы хотите, чтобы он выполнялся, но вы устанавливали блокировку для String и вызывали wait () поверх«это», вот что вызывает у вас исключение.