Re: How could i change this prog to wait() and notify()?
I made some changes but i ran into a deadlock. :sick:
Code:
import java.io.*;
class AlphaHolder{
private static boolean available = false;
public static void setAvailable(boolean a){
available = a;
}
public static boolean getAvailable(){
return available;
}
}
public class ThTest{
public static void main(String[] args){
AlphaSucker as = new AlphaSucker("AlphaSucker");
ThreadState(as);
as.start();
AlphaBlower ab = new AlphaBlower("AlphaBlower");
ThreadState(ab);
ab.start();
}
public static void ThreadState(Thread t){
Enum e = t.getState();
System.out.println(t.getName() + " is " + e.name());
}
}
class AlphaBlower extends Thread{
private BufferedWriter bw;
public AlphaBlower(String tname){
super(tname);
}
public synchronized void run(){
ThTest.ThreadState(Thread.currentThread());
try{
bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
for(int alpha = 97; alpha <= 122; ++alpha){
try{
bw.write(alpha);
}catch(IOException ioe){
System.err.println(ioe);
}
}
}finally{
try{
bw.close();
AlphaHolder.setAvailable(true);
notify();
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
}
class AlphaSucker extends Thread{
private int c;
private BufferedReader br;
public AlphaSucker(String tname){
super(tname);
}
public synchronized void run(){
ThTest.ThreadState(Thread.currentThread());
while(!AlphaHolder.getAvailable()){
try{
wait();
}catch(InterruptedException ie){
System.out.println(ie);
}
}
try{
br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
while((c = br.read()) != -1){
System.out.print((char)c);
}
}catch(IOException ioe){
System.err.println(ioe);
}finally{
try{
br.close();
}catch(IOException ioe){System.err.println(ioe);}
}
}
}
Re: How could i change this prog to wait() and notify()?
No more deadlock since i changed some things. I didn't make sense to have two separate threads each having a synchronized method. Nothing to synchronize on. :lol: I added an extra method (which is synchronized) to the AlaphaHolder class. Also an IllegalMonitorStateException is thrown at the notifyAll(). :sick:
Code:
import java.io.*;
class AlphaHolder{
private static boolean available = false;
public static void setAvailable(boolean a){
available = a;
}
public static boolean getAvailable(){
return available;
}
public static synchronized File getFile(){
return new File("C:" + File.separator + "AlphaHolder.txt");
}
}
public class ThTest{
public static void main(String[] args){
AlphaSucker as = new AlphaSucker("AlphaSucker");
ThreadState(as);
as.start();
AlphaBlower ab = new AlphaBlower("AlphaBlower");
ThreadState(ab);
ab.start();
}
public static void ThreadState(Thread t){
Enum e = t.getState();
System.out.println(t.getName() + " is " + e.name());
}
}
class AlphaBlower extends Thread{
private BufferedWriter bw;
public AlphaBlower(String tname){
super(tname);
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
try{
bw = new BufferedWriter(new FileWriter(AlphaHolder.getFile()));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
for(int alpha = 97; alpha <= 122; ++alpha){
try{
bw.write(alpha);
}catch(IOException ioe){
System.err.println(ioe);
}
}
}finally{
try{
bw.close();
AlphaHolder.setAvailable(true);
notifyAll();
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
}
class AlphaSucker extends Thread{
private int c;
private BufferedReader br;
public AlphaSucker(String tname){
super(tname);
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
while(!AlphaHolder.getAvailable()){
try{
wait();
}catch(InterruptedException ie){;}
}
try{
br = new BufferedReader(new FileReader(AlphaHolder.getFile()));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
while((c = br.read()) != -1){
System.out.print((char)c);
}
}catch(IOException ioe){
System.err.println(ioe);
}finally{
try{
br.close();
}catch(IOException ioe){System.err.println(ioe);}
}
}
}
Re: How could i change this prog to wait() and notify()?
Got the wait() and notify() to work but there seems no way to get one more thread state printed, namley WAITING.
Code:
import java.io.*;
class AlphaHolder{
private boolean available;
public synchronized void write(){
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
for(int alpha = 97; alpha <= 122; ++alpha){
try{
bw.write(alpha);
}catch(IOException ioe){
System.err.println(ioe);
}
}
}finally{
try{
bw.close();
available = true;
notifyAll();
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
public synchronized void read(){
while(!available){
try{
wait();
}catch(InterruptedException ie){;}
}
int c;
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
while((c = br.read()) != -1){
System.out.print((char)c);
}
}catch(IOException ioe){
System.err.println(ioe);
}finally{
try{
br.close();
}catch(IOException ioe){System.err.println(ioe);}
}
}
}
public class ThTest{
public static void main(String[] args){
AlphaHolder ah = new AlphaHolder();
AlphaSucker as = new AlphaSucker("AlphaSucker",ah);
ThreadState(as);
as.start();
AlphaBlower ab = new AlphaBlower("AlphaBlower",ah);
ThreadState(ab);
ab.start();
}
public static void ThreadState(Thread t){
Enum e = t.getState();
System.out.println(t.getName() + " is " + e.name());
}
}
class AlphaBlower extends Thread{
private AlphaHolder ah;
public AlphaBlower(String tname, AlphaHolder ah){
super(tname);
this.ah = ah;
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
ah.write();
}
}
class AlphaSucker extends Thread{
private AlphaHolder ah;
public AlphaSucker(String tname, AlphaHolder ah){
super(tname);
this.ah = ah;
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
ah.read();
}
}