java - why does my notify() not wake a waiting thread? -


I have a simple multithreaded algorithm, which is designed to load a series of files in the background thread, and Displays the first image of a JPanel as soon as it is being loaded in the creator of the JPN, I start the loader, then wait on the list of images, as follows:

  / / MyPanel.java public array list & lt; BufferedImage & gt; Picture = new arrelisted & lt; BufferedImage & gt; (); Int frame; Public MyPanel (string dior) {thread loader = new thread (new loader (Dior, this)); Loader.start (); Frame = 0; // Synchronize different stuff (images) {while (images.isEmpty ()) {images.wait (); }} This.setPrefferedSize (new dimension (800,800)); } @ Override Public Wide Paint Comonant (Graphics G) {SuperPaintConant (G) G Dreimage (images frame (frame), 0, 0, 800, 800, empty); }   

My loader thread looks like this:

  // Loader.java String dir; MyPanel Collar; Public Loader (String Deer, MyPassel Caller) {this.dir = dir; This.caller = Collar; } @ Override Public Wide Run (file {file file = new file (DIR); File [] files = dirFile.listFiles (); Arrays.sort (files); {File: Files} {Try {synchronize (caller. Image) {Buffer image buff image = imago Reed (F); Caller.images.add (buffImage); Caller.images.notifyAll (); }} Hold (IOException e) {}}}   

I have verified that execution is often used by notification () (usually> 20) The thread passes before calling, the thread awakens and displays the image in the frame. I have also verified that the picture object is actually the same object, which one is waiting for. I tried to add a yield () , but he did not help why does the call to inform () did not bend the thread waiting for instant?

I've verified that several times before the execution calling thread wakes up (usually & gt ; 20) informs all frames () and displays the image in the frame.

Your loader is thread looping, retrieving the instant monitor at caller.images , before it becomes its own Leave Timeslice.

Waiting for the pouch to retrieve the monitor, it can progress - it can not do this because the loader has caught it again.

It's not really clear what you are trying to achieve here, but start a new thread and then wait inside a producer usually a lot Bad idea, oh, if you can not do anything until the image loads for the first time, why not synchronize it? Are you doing anyway that you are effectively doing

Comments