I need implementation of methods suspend() and resume(), using wait() and notify(), but without using active waiting.
6 replies to this topic
#1
Posted 29 October 2011 - 10:02 AM
|
|
|
#2
Posted 29 October 2011 - 01:55 PM
What do you have so far?
#3
Posted 30 October 2011 - 07:05 AM
I don't have anything, that is a homework task..
It supposed to be as simple as possible..
And, it's for tomorrow, so please, help! :)
It supposed to be as simple as possible..
And, it's for tomorrow, so please, help! :)
#4
Posted 30 October 2011 - 07:11 AM
Try googling around for information on wait and notify in java. There are tons of resources on this subject.
#5
Posted 30 October 2011 - 07:22 AM
I tried, but I didn't find what I am looking for..
I wrote this peace of code, but I know it's not good, it should be process for
that is the suspend() called get to sleep, not process that is calling suspend(), and I do not know how to do it right.
import java.util.concurrent.*;
public class Domaci1 {
private boolean bad=false;
public synchronized void methodSuspend() {
while(bad=true) {
try {
wait();
} catch(InterruptedException e) {
}
}
}
public synchronized void methodResume() {
if(bad=false) {
notify();
}
}
}
I wrote this peace of code, but I know it's not good, it should be process for
that is the suspend() called get to sleep, not process that is calling suspend(), and I do not know how to do it right.
import java.util.concurrent.*;
public class Domaci1 {
private boolean bad=false;
public synchronized void methodSuspend() {
while(bad=true) {
try {
wait();
} catch(InterruptedException e) {
}
}
}
public synchronized void methodResume() {
if(bad=false) {
notify();
}
}
}
#6
Posted 30 October 2011 - 08:48 AM
I didn't actually run the code, but this would seem better. As you didn't really use your "bad" boolean a lot, or at least wrong.
You need 2x"=" to check for equality, otherwise you just change the boolean's value.
So something like this would already be a step closer.
But, to actually call wait and notifiy on an Object you need the Object's lock. This is done using the "synchronize" keyword. I am however unsure if you can actually synchronize yourself...
You need 2x"=" to check for equality, otherwise you just change the boolean's value.
So something like this would already be a step closer.
import java.util.concurrent.*;
public class Domaci1 {
private boolean suspend = false;
public synchronized void methodSuspend() {
while(suspend) {
try {
wait();
} catch(InterruptedException e) { }
}
}
public synchronized void methodResume() {
if(suspend) {
suspend = false;
notify();
}
}
}
But, to actually call wait and notifiy on an Object you need the Object's lock. This is done using the "synchronize" keyword. I am however unsure if you can actually synchronize yourself...
#7
Posted 30 October 2011 - 09:01 AM
Thank you very much for the solution, and thanks for reminding, I know it's a "==" but I've accidentally made a mistake..
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









