Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] freeze while requesting a URL

ProcessList is not thread safe.  Why have your own queue?  Why not just use
asyncExec()?



|---------+---------------------------------->
|         |           Moritz Angermann       |
|         |           <moritz.angermann@gmx.n|
|         |           et>                    |
|         |           Sent by:               |
|         |           platform-swt-dev-admin@|
|         |           eclipse.org            |
|         |                                  |
|         |                                  |
|         |           04/14/2003 03:21 PM    |
|         |           Please respond to      |
|         |           platform-swt-dev       |
|         |                                  |
|---------+---------------------------------->
  >---------------------------------------------------------------------------------------------------------------------------|
  |                                                                                                                           |
  |       To:       platform-swt-dev@xxxxxxxxxxx                                                                              |
  |       cc:                                                                                                                 |
  |       Subject:  Re: [platform-swt-dev] freeze while requesting a URL                                                      |
  |                                                                                                                           |
  >---------------------------------------------------------------------------------------------------------------------------|




Looks like i found a way to do this:
maybe this is something to post somewhere? ( i don't know if my code is
written good enough ) but it might help others who have the same
problem.
the basic idea is to have a ProcessList wich is a List of ProcessItems.
and returns on ProcessList.next() the next item in the queue or null.
the Item is of the type Runnable so it can get directly executed.
either via Display.getDefault.asyncExec(r) or via r.run in the mainloop,
if the execution bool's is set.

sorry for so few comments. i hope the code is mostly human readable.

[btw: would like feedback on this pice, coding style, usage of SWT
right|wrong, etc. - just if you like]

reg.
 moritz

//--- CODE [swtThreadTest.java] ---//

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

class ProcessItem{
             public ProcessItem prev = null;
             public ProcessItem next = null;
             public Runnable r = null;

             public ProcessItem(Runnable r){
                         this.r = r;
             }
}

class ProcessList{
             private ProcessItem first = null;
             private ProcessItem last = null;
             private int Processes = 0;

             public int length(){return this.Processes;}

             public void add(Runnable r){
                         ProcessItem p = new ProcessItem(r);
                         if(this.first == null && this.last == null){
                                     this.first = p;
                                     this.last = p;
                         }else{
                                     p.prev = this.last;
                                     this.last.next = p;
                                     this.last = p;
                         }
                         this.Processes++;
             }
             public Runnable next(){
                         if(this.first != null){
                                     Runnable r = this.first.r;
                                     if( this.first != this.last ){
                                                 // first is not the same
as last,
                                                 // so we just set first to
the next element.
                                                 this.first =
this.first.next;
                                     } else {
                                                 // we just returned the
last element.
                                                 // now we need to clear
out the queue.
                                                 this.first = null;
                                                 this.last = null;
                                     }
                                     this.Processes--;
                                     return r;
                         }else{
                                     return null;
                         }
             }
}

public class swtThreadTest {
             static int ProcessNum = 0;
             static boolean DisplayQueueHandlesProcesses = false;

             public static void main (String [] args) {
                         final ProcessList List = new ProcessList();
                         final Display display = new Display ();
                         final Shell shell = new Shell (display);
                         shell.setText("Using the DisplayQueue for
execution of queued
Processes...");
                         GridLayout Layout = new GridLayout();
                         Layout.numColumns = 3;
                         shell.setLayout(Layout);

                         Button b = new Button(shell,SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("New Process");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){

System.out.println("adding Process:" + List.length());
                                                             Runnable r =
new Runnable(){

public void run(){

       System.out.println("Finishing Process");
                                                                         }
                                                             };
                                                             List.add(r);
                                                 }
                                     });
                         b = new Button(shell,SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("Print Number of Processes");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){

System.out.println("Processes: "+ List.length());
                                                 }
                                     });
                         b = new Button(shell, SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("Exec next");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){
                                                             Runnable r =
List.next();
                                                             if(r != null)
       display.asyncExec(r);
                                                             else
System.out.println("There is no Process left!");
                                                 }
                                     });
                         b = new Button(shell, SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("Enable MainQueue Usage");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){

DisplayQueueHandlesProcesses = true;
                                                 }
                                     });
                         b = new Button(shell, SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("Disable MainQueue Usage");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){

DisplayQueueHandlesProcesses = false;
                                                 }
                                     });
                         b = new Button(shell, SWT.PUSH);
                         b.setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
                         b.setText("Quit");
                         b.addListener( SWT.MouseDown,
                                     new Listener(){
                                                 public void handleEvent(
Event e ){
                                                             shell.dispose
();
                                                 }
                                     });

                         shell.pack();
                         shell.open ();

                         while (!shell.isDisposed ()) {
                                     if(DisplayQueueHandlesProcesses){
                                                 Runnable r = List.next();
                                                 if(r != null){

System.out.println("executing runnable");
                                                             r.run();
                                                 }
                                     }
                                     if (!display.readAndDispatch ())
display.sleep ();
                         }
                         display.dispose ();
             }
}
//--- CODE ---//

On Mon, 2003-04-14 at 19:09, Moritz Angermann wrote:
> well what i've been now working on half the day was
> to get my URL fetch function into a thread that requests a page
> and tells me when it's done.
> I get errors about invalid Thread access, etc.
>
> previous to my tryes i had the URL request in the MainThread ( UIThread
> )
> and thus it froze my UI each time i requested a URL.
>
> finally i kind of switch a boolean on/off. in the MainThread from the
> URLThread. but this seems not to be right.
>
> I think what i'd need to do is to queue up the MainThread for
> processing.the stuff.
>
> Or should i just go the other way round and let the MainThread(UI) queue
> up my seperat thread?
>
> But than again, how do i access the widgets to set the result?
> will allways return in invalid thread access  :*/
>
> thanks in advance,
>  moritz angermann
>
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
--

_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev






Back to the top