Skip to main content

multiple tasks

6 replies [Last post]
vlie84
Offline
Joined: 2003-12-02
Points: 0

Hey,

at the moment i have a task and inside the task i have a timer that will stop the first task and do this second task, once the seond task finish it will continue doing the first task again.this timer is every 5 seconds.
the problem i'm having is the second task is consuming alots of time. is there a way to allow these 2 task running in the same time?

task 1 is a Swing program, while task 2 basically saving image pick by user in task 1.

can you give me some code

thank you so much

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Scott Violet

On Wed, Dec 03, 2003 at 09:23:50PM -0500, swing-feedback@javadesktop.org wrote:
> Hey,
>
> at the moment i have a task and inside the task i have a timer that
> will stop the first task and do this second task, once the seond
> task finish it will continue doing the first task again.this timer
> is every 5 seconds.
> the problem i'm having is the second task is consuming alots of
> time. is there a way to allow these 2 task running in the same time?

Certainly. Create a separate thread for each of the tasks and run
them concurrently. If for some reason you need task 1 to stop after a
certain amount of time and wait until task 2 is finished you'll need
to have it block until some value is set, eg:

[code]
runTask1() {
runFor5Seconds();
// wait until tast2 is done
synchronized(lock) {
while (!task2Done) {
lock.wait();
}
}
// task2 has finished
// continue running
runForMoreThan5Seconds();
}
runTask2() {
doStuff();
// Task 2 is done, set flag to wake up anyone blocked on me
synchronized(lock) {
task2Done = true
lock.notifyAll();
}
}
[/code]

> task 1 is a Swing program, while task 2 basically saving image pick
> by user in task 1.

Hopefully my previous snippets are applicable to your situation.

-Scott

vlie84
Offline
Joined: 2003-12-02
Points: 0

hey scott thank you for your suggestion. im still new with this thread thingy, so can you guide me one by one please....
i get how your program will work, but what is lock??

zander
Offline
Joined: 2003-06-13
Points: 0

Hi,

The variable 'lock' is an object. It can be any object; but in this case you probably want it to be the task.

vlie84
Offline
Joined: 2003-12-02
Points: 0

hey thank you so much, it work great using the thread
but theres problem after that.
when i want to open the file that i save it come up with
IllegalStateException:unread block data

do you have any idea why?

shan-man
Offline
Joined: 2006-02-17
Points: 0

vlie84 wrote:
> hey thank you so much, it work great using the
> thread
> but theres problem after that.
> when i want to open the file that i save it come up
> with
> IllegalStateException:unread block data
>
> do you have any idea why?

I don't. But, as I have mentioned previously on another thread, posting compilable and runnable source code may help... ;)

Regards,
Shannon

vlie84
Offline
Joined: 2003-12-02
Points: 0

hey sorry i tried it again, now the task is working properly.i have problem in my other code.
but my problem is still the same. my second task is saving a stack that is used by task1.and this saving takes ages. so task 1 have to wait for ages.
i tried to make them run concurrently but the other problem come is if task1 finished before task2 , the task2 will stop prematurely.