Thanks your version with the Queues works great.
regards.
Thanks your version with the Queues works great.
regards.
Who even told you this was homework?
And if it was - what it isn't - they are right, i am not asking to solve my problem, but what a good way to achieve what i want is.
And who tells you I'm from a country where i as supposedly lazy student would steal places from other to be students?
But as this isn't homework...
The code i posted is just some testing code, the context it is needed in is a huge webapp that I'm employed to work on.
Yes I am a student, I had all the mutex, semaphore theory, I also know some things about java synchronisation. But what I know isn't enough to solve my problem.
Like you sure know is that you can't just apply theory to practical things.
And i dont like to reinvent the weel if it has already been implemented (like thes lock things).
And now since it's the next workday for me since first posting this. I will read and hopefully learn about all the things the more helpful of posters told me about.
If i got more questions i will ask again, and else ill post the final code that works for me.
Thanks :)
@asuffield said:
Do your own homework.
If you think this is below your level just troll somewhere else. Thank you.
The custom container is not the point here because it could be anything, a single object, whatever. It's just an example.
I'll get back when i tried your hints.
Thanks.
I haven't looked into java.util till now. Will do though.
Thanks for the help.
Hi,
I have to write a little program which in fact resembles the "producer and consumer" problem.
But there's a little extra to it, the producer has to check for a timeout.
I have writte a little incomplete example to show you what i mean:
public class Observer implements Runnable {
private final static long CYCLE_TIME = 10000;
private final static long MAX_TIME = 180000;
private final static long TIMEOUT = MAX_TIME + CYCLE_TIME;
private final Container container; //Some self-defined container
private Something SOMETHING = new Something("foo"); //whatever ;)
private Thread workerThread;
public Observer() throws {
container = new Container();
worker = new Worker(container);
workerThread = new Thread(worker);
}
public void run() {
while(!Thread.currentThread().isInterrupted()) {
workerThread.start();
synchronized (container) {
container.add(SOMETHING);
container.notify();
container.wait(TIMEOUT); //There's a little Problem now see * in posting
if(/*something like timeout hit == true*/) {
//timeout detected
//do stuff
} else {
//all working properly
}
}
}
}
}
public class Worker implements Runnable {
private final Container container;
private Something something;
public Worker(Container container) {
this.container = container;
}
public void run() {
while(!Thread.currentThread().isInterrupted()) {
synchronized (container) {
container.wait();
if(!container.isEmpty()) {
something = container.get();
} else {
continue;
}
//do something with something ;)container.remove();
container.notify();
}
}
}
}
So you see that i synchronize over the container that both threads have in common.
I think i managed to get mutual exclusion in there, if not please help me make this better.
*But the real Problem is, how to find out that TIMEOUT was hit. I mean i could get a timestamp before add TIMEOUT and check if its in close boundaries, and if container is empty.
But thats a bit fuzzy and in don't tink thats a good way to do this.
To get to the point:
What i want to achieve is a producer, it only produces one item, no queu, and lets the consumer work on it. But the consumer mustn't take longer than TIMEOUT to achieve his job.
I have to detect this. Also there must be mutual exclusion over the container.
I see a few problems of which im not sure they even are problems :)
Rather i feel uncomfortable with this stuff since i am not very experienced in parallel programming. So if you got help, hints, tips or anything else...
Thanks in advance,
nFec
Yeah i know, like stated the whole project is (except its goal) a major WTF.
But i got no saying in this, i even DROPPED the production database (after a backup) via SQL injection FROM HOME.
I pointed out how it worked etc. first my boss was stunned, cause he got no clue, then he went angry and then he told me: "Well youre an expert, nobody else will know how to do this."
Well i could rant about it all day, but the thing is, i need to finish this assignment; it might be ugly but i dont want to produce new problems.
So what youre basically saying is to put a try. catch around the whole execution code?
Shouldn't be a problem, cause this only "logs out" users, what is only checked for displaying rather than security issues.
Yeh WTF again...
[quote user="ammoQ"]
For those reasons, I would not use Thread.interrupt() unless I really really have to or I know for sure that the thread is in sleep() or wait().
Because I don't know (and don't want to learn the hard way) how a JDBC driver or something similar behaves when being interrupted... It might wrack havoc to the database connection, so the cleanup cannot be done.
[/quote]
Well but i need to interrupt it, because if the Servlet gets destroyed that means, the Webserver is going down; and i need to clean up and stop my work then.
Thanks for your help!
I'll try the catch... break; and cleanup after the loop thingie.
@ extending Thread vs. implement Runnable:
I personally don't like to implement Runnable cause this is ugly imho: Thread t = new Thread(myRunnableObject); t.start();
I'd only implement Runnable if i had to extend from another class too.
But thats not the point here ;)
Regards
nFec
Well IF they dont want integer division in there thats not really WTFish.
The division and multiplication is done in floating point arithmetic and then its converted to int again.
You could argue that this is very ressource hungry, that it is rarely needed, etc. yadda yadda; BUT IF this is intentional then its not a WTF!
Hi there,
me and a friend are sitting at work and just can't grasp the following:
I got a HttpServlet which is started on webapp launch and then starts my Thread, on its destroy-method the Servlet should call myThread.interrupt(); to clean things up.
The Thread itself is doing a DB-query periodically (don't ask, the whole webapp is a major WTF with tons of batch files, servlets, harcoded passwords, cleartext passwords in the db... you get the picture), so it has a connection to the database. If it gets interrupted i'd like to close the connection and shut down nicely.
Now i realise you normally would code a bunch of listeners, events, queus, etc. but its a single thread in an wtf environment.
So to get to the point: Would this work?
Would that work? Or how would you do it?
Im not that fit in Threading and i realise that this might be very bad style, or even not working; but thats why I'm asking you.
Regards,
nFec