Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question:multithreading and shared value

Status
Not open for further replies.

victor1

Programmer
May 2, 2000
2
US
I am a newbie, and I have a question about multithreading: <br>if I need several threads, i.e.: 7 threads, they all share<br>an int value. Three of them will check if this value is less<br>than 0, if so, increase it by adding a ramdom value; other threee will check if it is bigger than 0, if so, they will decrease it by using a random value; that another thread makes sure that value will not be zero. Can anyone give me an idea? Do I need to create 3 different thread classes<br>or just declare 7 threads under Thread? Thx in advance.
 
Hi!<br><br>Here is a little example, safe stack implementation. I think it will help you...<br><br>Bye, Otto.<br>*************************************<br>class WN2 {<br>&nbsp;&nbsp;static MyStack ms=new MyStack();<br>&nbsp;&nbsp;final static int STACK_SIZE=10;<br><br>&nbsp;&nbsp;public static void main(String[] args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;WN2 wn=new WN2();<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;WN2() {<br>&nbsp;&nbsp;&nbsp;&nbsp;Thread th;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;th=new Thread(new Fogyaszto());<br>&nbsp;&nbsp;&nbsp;&nbsp;th.setPriority(Thread.NORM_PRIORITY);<br>&nbsp;&nbsp;&nbsp;&nbsp;th.start(); // start a thread to pop values<br>&nbsp;&nbsp;&nbsp;&nbsp;th=new Thread(new Termelo());<br>&nbsp;&nbsp;&nbsp;&nbsp;th.setPriority(Thread.MIN_PRIORITY);<br>&nbsp;&nbsp;&nbsp;&nbsp;th.start();&nbsp;&nbsp;// start a thread to push values<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;class Termelo implements Runnable {<br>&nbsp;&nbsp;&nbsp;&nbsp;public void run() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (true) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ms.push('P');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;class Fogyaszto implements Runnable {<br>&nbsp;&nbsp;&nbsp;&nbsp;public void run() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (true) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ms.pop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>}<br>class MyStack {<br>&nbsp;&nbsp;int idx=0;<br>&nbsp;&nbsp;char[] data=new char[WN2.STACK_SIZE];<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;synchronized void push(char c) {<br>&nbsp;&nbsp;&nbsp;&nbsp;while (idx==WN2.STACK_SIZE) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Wait to push...&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) { System.out.println(&quot;Wait error.&quot;); }<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;data[idx++]=c;<br>&nbsp;&nbsp;&nbsp;&nbsp;notify();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;synchronized char pop() {<br>&nbsp;&nbsp;&nbsp;&nbsp;while (idx==0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Wait to pop...&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) { System.out.println(&quot;Wait error.&quot;); }<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;char c=data[--idx];<br>&nbsp;&nbsp;&nbsp;&nbsp;notify();<br>&nbsp;&nbsp;&nbsp;&nbsp;return c;<br>&nbsp;&nbsp;}<br>}<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top