Results 1 to 5 of 5

Thread: Help needed for Background Tasks in Java!

  1. #1
    Join Date
    Aug 2006
    Posts
    114

    Help needed for Background Tasks in Java!

    Hi friends,
    I have recently completed the C++ programming language and turned towards the Java, so not having much knowledge of Java. I have created a GUI With JFC. I have also used the SwingWorker for doing some application. All I want to know is about the Background Tasks in Java! Hope that somebody would response me sooner.!!
    |===================|
    |YAY if that made sense...|
    |===================|

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: Help needed for Background Tasks in Java!

    Many times you use the TumbleItem applet for loading the set of graphic files used in an animation. There are chances of getting delayed before the GUI appears, if the graphic files are loaded from an initial thread. And the GUI may be unresponsive temporarily, the graphic files are loaded from the event dispatch thread. The object's doInBackground method, executing in a worker thread, loads the images into an ImageIcon array, and returns a reference to it.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: Help needed for Background Tasks in Java!

    You can retrieve the object returned by doInBackground in two ways which are described as follows :
    1. SwingWorker.get - You will have to invoke this method without any arguments. If the background task is not finished, get blocks until it is.
    2. SwingWorker.get - You will have to invoke this method with arguments indicating a timeout. This blocks until the background task is not finished, unless the timeout expires first, in which case get throws java.util.concurrent.TimeoutException.
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Help needed for Background Tasks in Java!

    I have provided you the coding that defines and executes the SwingWorker object. The following coding does the same :
    Code:
    SwingWorker sworker = new SwingWorker<ImageIcon[], Void>() {
        @Override
        public ImageIcon[] doInBackground() {
            final ImageIcon[] innerImgs = new ImageIcon[imgico];
            for (int i = 0; i < imgico; i++) {
                innerImgs[i] = loadImage(i+1);
            }
            return innerImgs;
        }
    
        @Override
        public void done() {
            animator.removeAll();
            loopslot = -1;
            try {
                imgs = get();
            } catch (InterruptedException ignore) {}
            catch (java.util.concurrent.ExecutionException e) {
                String why = null;
                Throwable cause = e.getCause();
                if (cause != null) {
                    why = cause.getMessage();
                } else {
                    why = e.getMessage();
                }
                System.err.println("Error while retrieving file: " + why);
            }
        }
    };

  5. #5
    Join Date
    Mar 2008
    Posts
    672

    Re: Help needed for Background Tasks in Java!

    I have gone through the post which was posted by 'Viensterrr'. He has described the two ways for retrieving the object returned by doInBackground which is correct. But you should keep in mind when invoking either overload of get from the event dispatch thread; until get returns, no GUI events are being processed, and the GUI is "frozen".Don't invoke get without arguments unless you are confident that the background task is complete or close to completion.

Similar Threads

  1. Replies: 6
    Last Post: 23-08-2012, 03:22 PM
  2. NIS 2011: background tasks resulting in constant disk activity
    By maheja in forum Networking & Security
    Replies: 4
    Last Post: 03-01-2011, 06:54 AM
  3. How to disable background tasks in Office 2010?
    By Bharata in forum Windows Software
    Replies: 3
    Last Post: 16-10-2010, 05:48 AM
  4. Set ToolTip Text background color in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 24-02-2010, 11:05 PM
  5. JAVA Help - Background change by color matching
    By Daren in forum Software Development
    Replies: 4
    Last Post: 12-05-2009, 08:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,918,712.73644 seconds with 17 queries