Synchronization question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Synchronization question

Mathias Versichele
Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:

private Process process;

...

for loop {

         process = new Process(...)
         process.execute(new TaskAdapter<String>(tl));

         other code...
}

the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).

I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?

Regards

--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Greg Brown
Administrator
Tasks are a great way to run a background process. I am wondering why are you creating and starting them in a loop? Are you trying to execute more than one?

On Feb 24, 2010, at 8:19 AM, Mathias Versichele wrote:

> Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:
>
> private Process process;
>
> ...
>
> for loop {
>
>          process = new Process(...)
>          process.execute(new TaskAdapter<String>(tl));
>
>          other code...
> }
>
> the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).
>
> I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?
>
> Regards
>
> --
> Mathias Versichele
> Bio-ir milieutechnologie / Msc. geografie
> Oudburgstraat 16
> 9240 Zele
> 0485/16.07.08

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Todd Volkert
In reply to this post by Mathias Versichele
Mathias,

is there any way you can give a more complete code sample?  It might shed some light on what's going on.

-T

On Wed, Feb 24, 2010 at 8:19 AM, Mathias Versichele <[hidden email]> wrote:
Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:

private Process process;

...

for loop {

         process = new Process(...)
         process.execute(new TaskAdapter<String>(tl));

         other code...
}

the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).

I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?

Regards

--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Mathias Versichele
In reply to this post by Greg Brown
I'm parsing files in a directory. Logically speaking, it's a task consisting of synchronized subtasks but I thought that would make it even more complex for me :-)

2010/2/24 Greg Brown <[hidden email]>
Tasks are a great way to run a background process. I am wondering why are you creating and starting them in a loop? Are you trying to execute more than one?

On Feb 24, 2010, at 8:19 AM, Mathias Versichele wrote:

> Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:
>
> private Process process;
>
> ...
>
> for loop {
>
>          process = new Process(...)
>          process.execute(new TaskAdapter<String>(tl));
>
>          other code...
> }
>
> the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).
>
> I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?
>
> Regards
>
> --
> Mathias Versichele
> Bio-ir milieutechnologie / Msc. geografie
> Oudburgstraat 16
> 9240 Zele
> 0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Greg Brown
Administrator
You may find the TaskGroup or TaskSequence classes helpful. They allow you to coordinate multiple running tasks. TaskGroup runs multiple tasks in parallel, and TaskSequence runs them in series.

On Feb 24, 2010, at 8:31 AM, Mathias Versichele wrote:

I'm parsing files in a directory. Logically speaking, it's a task consisting of synchronized subtasks but I thought that would make it even more complex for me :-)

2010/2/24 Greg Brown <[hidden email]>
Tasks are a great way to run a background process. I am wondering why are you creating and starting them in a loop? Are you trying to execute more than one?

On Feb 24, 2010, at 8:19 AM, Mathias Versichele wrote:

> Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:
>
> private Process process;
>
> ...
>
> for loop {
>
>          process = new Process(...)
>          process.execute(new TaskAdapter<String>(tl));
>
>          other code...
> }
>
> the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).
>
> I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?
>
> Regards
>
> --
> Mathias Versichele
> Bio-ir milieutechnologie / Msc. geografie
> Oudburgstraat 16
> 9240 Zele
> 0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Mathias Versichele
Ok, I'm trying my luck with the TaskSequence class. I'm running into another problem though... After I've added my tasks, I get a problem with this line of code:

ts.execute(ta);

ts is an instance of TaskSequence
ta is an instance of TaskAdapter<String>

I get this error compilation error in eclipse: 
"The method execute(TaskListener<Void>) in the type Task<Void> is not applicable for the 
 arguments (TaskAdapter<String>)"

I know this has something to do with the Void type, where there should be String but I dont know what is causing this

Regards

2010/2/24 Greg Brown <[hidden email]>
You may find the TaskGroup or TaskSequence classes helpful. They allow you to coordinate multiple running tasks. TaskGroup runs multiple tasks in parallel, and TaskSequence runs them in series.

On Feb 24, 2010, at 8:31 AM, Mathias Versichele wrote:

I'm parsing files in a directory. Logically speaking, it's a task consisting of synchronized subtasks but I thought that would make it even more complex for me :-)

2010/2/24 Greg Brown <[hidden email]>
Tasks are a great way to run a background process. I am wondering why are you creating and starting them in a loop? Are you trying to execute more than one?

On Feb 24, 2010, at 8:19 AM, Mathias Versichele wrote:

> Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:
>
> private Process process;
>
> ...
>
> for loop {
>
>          process = new Process(...)
>          process.execute(new TaskAdapter<String>(tl));
>
>          other code...
> }
>
> the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).
>
> I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?
>
> Regards
>
> --
> Mathias Versichele
> Bio-ir milieutechnologie / Msc. geografie
> Oudburgstraat 16
> 9240 Zele
> 0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Synchronization question

Greg Brown
Administrator
Try using TaskAdpater<Void> - TaskSequence extends Task<Void> (it doesn't return a value). The return values are provided by the tasks in the sequence, which you can access via the get() method when the task is complete.

On Feb 24, 2010, at 9:14 AM, Mathias Versichele wrote:

Ok, I'm trying my luck with the TaskSequence class. I'm running into another problem though... After I've added my tasks, I get a problem with this line of code:

ts.execute(ta);

ts is an instance of TaskSequence
ta is an instance of TaskAdapter<String>

I get this error compilation error in eclipse: 
"The method execute(TaskListener<Void>) in the type Task<Void> is not applicable for the 
 arguments (TaskAdapter<String>)"

I know this has something to do with the Void type, where there should be String but I dont know what is causing this

Regards

2010/2/24 Greg Brown <[hidden email]>
You may find the TaskGroup or TaskSequence classes helpful. They allow you to coordinate multiple running tasks. TaskGroup runs multiple tasks in parallel, and TaskSequence runs them in series.

On Feb 24, 2010, at 8:31 AM, Mathias Versichele wrote:

I'm parsing files in a directory. Logically speaking, it's a task consisting of synchronized subtasks but I thought that would make it even more complex for me :-)

2010/2/24 Greg Brown <[hidden email]>
Tasks are a great way to run a background process. I am wondering why are you creating and starting them in a loop? Are you trying to execute more than one?

On Feb 24, 2010, at 8:19 AM, Mathias Versichele wrote:

> Ok, this might be the most difficult question until now from me (especially since I can't seem to fully understand synchronization). I'm using a Task class to run a background task asynchronously, so that the UI stays responsive and my activityMeter keeps me informed about the progress of the task. The problem now is, that somewhere I do this:
>
> private Process process;
>
> ...
>
> for loop {
>
>          process = new Process(...)
>          process.execute(new TaskAdapter<String>(tl));
>
>          other code...
> }
>
> the Process class extends the Task class. If my for loop gets called only once everything works perfect. Otherwise, though, the code keeps running and the process variable gets re-initialized with every loop until finally only the last run of the loop makes the process run completely (with my UI acting accordingly). What do I have to do to make the code stop after the process.execute() line ? I already tried the wait and notify methods, but they gave me errors until I used the synchronized keyword. Then, however, the processes run correctly but my UI freezes (indefinitely).
>
> I'm kind of stuck here... is using the Task class the right way to go or are there better ways for running threads in the background keeping my UI nice and healthy ?
>
> Regards
>
> --
> Mathias Versichele
> Bio-ir milieutechnologie / Msc. geografie
> Oudburgstraat 16
> 9240 Zele
> 0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08




--
Mathias Versichele
Bio-ir milieutechnologie / Msc. geografie
Oudburgstraat 16
9240 Zele
0485/16.07.08

Loading...