First of all, I used Runnabal and created 'true' loop to continue my work . Now I find some difficulty when changing to use callable.
Package Com; Import java.util.concurrent.Callable; Import java.util.concurrent.ExecutionException; Import java.util.concurrent.FutureTask; Public Class CallableAndFuture {Public Static Zero Main (String [] Args) halts exceptions, execution exceptions {ChildCallbale childCallbale = new ChildCallbale (); FutureTask & LT; Integer & gt; Future = New Future Task & lt; Integer & gt; (ChildCallbale); Thread thread = new thread (future); Thread.start (); ChildCallbale.setVar (1); Println (future.get ()); ChildCallbale.setVar (2); Println (future.get ()); }} Class ChildCollBell enforces callable & lt; Integer & gt; {Private Ent. Public Zero Set (Int World) {this.var = var; } @ Override Public Integer Call () throws exception {Thread.sleep (2000); Return var; }}
As you see, I want to get different results as I did, unfortunately, 2 results are equal to 1. Not only do I want to know how to know my needs, I also want to know why my code goes wrong. thank you in advanced.
get a () To do so it waits for two seconds of your thread. Then the result is 1. And the result is 1, because the thread is not run again (and should not run again!).
Then the second setVar
has no effect on the result.
Comments
Post a Comment