dcsimg
Thread Synchronization using wait() and notify()
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sakshi_Anand
Posted On:   Saturday, May 20, 2006 06:24 PM

Hi.. I am an absolute naive in java, however, unfortunately i hv to complete my project in it. Wonderng if nybdy can help me.... I hv got three classes. I want to run the main thread and one another thread (say second thread) simultaneously, wen the program starts. The so called second thread, should halt in between for some values (that needs to be computed by the main thread) [ I m tryng to implement this using wait () and notify()] and then resumes (aftr the values it was waitng fr has been computed by the main thread)..However, unsuccessful.. my three classes are as follows: file, key and voicee. the code is as follow: The File class public class File extends JPanel implements K   More>>

Hi..

I am an absolute naive in java, however, unfortunately i hv to complete my project in it. Wonderng if nybdy can help me....

I hv got three classes. I want to run the main thread and one another thread (say second thread) simultaneously, wen the program starts. The so called second thread, should halt in between for some values (that needs to be computed by the main thread) [ I m tryng to implement this using wait () and notify()] and then resumes (aftr the values it was waitng fr has been computed by the main thread)..However, unsuccessful..

my three classes are as follows: file, key and voicee.

the code is as follow:

The File class


public class File extends JPanel implements KeyListener {

			
JTextField typingArea;
BufferedReader bf;
final int total;
int current=-1;
boolean error = false;
boolean value = false;
String s;

int present = -1;

// use own key classes
Key uk = new Key(Key.DOWN_KEY);
voicee v;

LinkedList artists = new LinkedList();


public File(String input)
{
			
super(new BorderLayout());

typingArea = new JTextField("Press Down Button to Start", 20);
typingArea.addKeyListener(this);
typingArea.setEditable(false);
add(typingArea, BorderLayout.CENTER);

bf = getFile(input);
this.getArtistsList();
total = artists.size()-2;
uk.getTotal(total);
}




public void keyTyped(KeyEvent e){

}



public void keyPressed(KeyEvent e) {

			
if(e.getKeyCode()==38 || e.getKeyCode()==40){
uk.press(present, e.getKeyCode());
}

}


public synchronized void keyReleased(KeyEvent e) {
			
if(e.getKeyCode()==38 || e.getKeyCode()==40)
{
present = uk.release();
value = true;
System.out.println("Present = " + present + " Artist = " + artists.get(present));
s = artists.get(present).toString();
v.val(value,s);
notify();
}

}

/**
* Create the GUI and show it.
*/

private static void GUI(String fileName)
{
			
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Instructions");
JFrame.setSize(100,100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel panel = new File(fileName);
panel.setOpaque(true);
frame.setContentPane(panel);

//Display the window
frame.pack();
frame.setVisible(true);
}





/* This method reads the file from the input
*/


public BufferedReader getFile(String in)
{
			
BufferedReader br = null;
FileReader fr = null;
try
{
fr = new FileReader(in);
br = new BufferedReader(fr);
}

catch (IOException e)
{
System.out.println("Error reading from " + in + " - Exiting...
");
e.printStackTrace();
System.exit(1);
}
return br;
}




/* This methods gets all the artist names from
* the file and stores them in a linked list
*/

public void getArtistsList()
{
			
String x;
try
{
do
{
x = bf.readLine(); artists.add(x);
}while(x!=null);
}

catch (IOException e)
{
// catch possible io errors from readLine()
System.out.println("Sorry, got a readline error!");
e.printStackTrace();
}
}





// Main method

public static void main(String[] args)
{
			
new voicee();
if (args.length != 1)
{
System.out.println("Usage: java File
");
System.exit(1);
}
final String fileName = args[0];
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI(fileName);
}
});


}
}




The voicee class


public class voicee implements Runnable {

			
String voice1, voiceName, name;
File f;
public boolean v = false;
Thread t;

voicee(String st, boolean vv)
{
voice1 = st;
v = vv;

}
public void val(boolean val, String s)
{
v =val;
voice1 =s;
}

voicee()
{
t = new Thread (this);
t.start();
}

synchronized public void run()
{
voiceName = "kevin16";
VoiceManager voiceManager = VoiceManager.getInstance();
Voice voice = voiceManager.getVoice(voiceName);

if (voice == null) {
System.err.println(
"Cannot find a voice named "
+ voiceName + ". Please specify a different voice.");
System.exit(1);
}

while (v=false)
{
try{
wait();
}catch(InterruptedException e ){
System.out.println("Interrupted Exception caught");
}
}
voice.allocate();
voice.speak(voice1);
voice.deallocate();
}

}




I am sorry about the long code. I need help in connecting these two threads, as I want the main thread (which is called on the file class) to notify the second thread (which is called on voicee class)under keyreleased (), aftr the desired character has been read by the console, so tht it can be read by my voicee class (by providng speech interface).

Thanx

   <<Less

Re: Thread Synchronization using wait() and notify()

Posted By:   WarnerJan_Veldhuis  
Posted On:   Sunday, May 21, 2006 05:24 AM

Search google for "producer consumer". That will get you started...

On a side note: would you type full words next time?? It makes it easier to read the question, and therefore more likely to get answers on. Cuz f u tlk lk ths, nbdy cn read it n nbdy wll tk the ffrt of ansrng ur q.
About | Sitemap | Contact