Given:
2. class MyThread implements Runnable { 3. public void run() { 4. for(int i = 0; i < 1000; i++) { 5. System.out.print(Thread.currentThread().getId() + "-" + i + " "); 6. } } } /*from w w w . j av a 2 s.c om*/ 7. public class Main { 8. public static void main(String[] args) throws Exception { 9. Thread t1 = new Thread(new MyThread()); 10. // insert code here 11. } 12. }
Which of the following code fragments, inserted independently at line 10, will probably run most (or all) of the main thread's run()
method invocation before running most of the t1 thread's run()
method invocation? (Choose all that apply.).
A. t1.setPriority(1); // w ww.j a v a2 s . com new MyThread().run(); t1.start(); B. t1.setPriority(9); new MyThread().run(); t1.start(); C. t1.setPriority(1); t1.start(); new MyThread().run(); D. t1.setPriority(8); t1.start(); new MyThread().run();
A, B, and C are correct.
For A and B, the main thread executes the run()
method before it starts t1.
C is correct because t1 is set to a low priority, giving the main thread scheduling priority.
D is incorrect because by setting t1's priority to 8, the t1 thread will tend to execute mostly before the main thread.