Hi Everyone,
I am currently reading a book on multi-threading and up until recently I have been able to understand what is going on. The thing is the complexity of the code has just jumped up about two gears without warning. The code is now using inner classes which I am trying to develop an understanding of but I am not finding it easy going, and the book has been lite on explanations. If anybody can help with the following code it will be really appreciated.
public class SetPriority extends Object{ private static Runnable makeRunnable() { Runnable r = new Runnable() { public void run() { for(int i=0; i<5; i++) { Thread t = Thread.currentThread(); System.out.println("in run() - priority=" + t.getPriority() + ", name=" + t.getName()); try{ Thread.sleep(2000); }catch(InterruptedException x){ //ignore } } } }; return r; } public static void main(String[] args) { Thread threadA = new Thread(makeRunnable(), "threadA"); threadA.setPriority(8); threadA.start(); Thread threadB = new Thread(makeRunnable(), "threadB"); threadB.setPriority(2); threadB.start(); Runnable r = new Runnable() { public void run() { Thread threadC = new Thread(makeRunnable(), "threadC"); threadC.start(); } }; Thread threadD = new Thread(r, "threadD"); threadD.setPriority(7); threadD.start(); try{ Thread.sleep(3000); }catch(InterruptedException x){ //ignore } threadA.setPriority(3); System.out.println("in main() - threadA.getPriority()=" + threadA.getPriority()); }}
My greatest challenge is understanding how the makeRunnable() method works. I don't understand how this inner class can be declared static and then multiple "instances" created from it. I know that I have no idea what is going on, please help!!!
Thanks for your time.
Regards
Davo
P.S.: If you know of any really good references on inner classes, particularly URL resources, please let me know. Thanks again.
I am currently reading a book on multi-threading and up until recently I have been able to understand what is going on. The thing is the complexity of the code has just jumped up about two gears without warning. The code is now using inner classes which I am trying to develop an understanding of but I am not finding it easy going, and the book has been lite on explanations. If anybody can help with the following code it will be really appreciated.
public class SetPriority extends Object{ private static Runnable makeRunnable() { Runnable r = new Runnable() { public void run() { for(int i=0; i<5; i++) { Thread t = Thread.currentThread(); System.out.println("in run() - priority=" + t.getPriority() + ", name=" + t.getName()); try{ Thread.sleep(2000); }catch(InterruptedException x){ //ignore } } } }; return r; } public static void main(String[] args) { Thread threadA = new Thread(makeRunnable(), "threadA"); threadA.setPriority(8); threadA.start(); Thread threadB = new Thread(makeRunnable(), "threadB"); threadB.setPriority(2); threadB.start(); Runnable r = new Runnable() { public void run() { Thread threadC = new Thread(makeRunnable(), "threadC"); threadC.start(); } }; Thread threadD = new Thread(r, "threadD"); threadD.setPriority(7); threadD.start(); try{ Thread.sleep(3000); }catch(InterruptedException x){ //ignore } threadA.setPriority(3); System.out.println("in main() - threadA.getPriority()=" + threadA.getPriority()); }}
My greatest challenge is understanding how the makeRunnable() method works. I don't understand how this inner class can be declared static and then multiple "instances" created from it. I know that I have no idea what is going on, please help!!!
Thanks for your time.
Regards
Davo
P.S.: If you know of any really good references on inner classes, particularly URL resources, please let me know. Thanks again.