Lab for Chapter Seven: Threads

Consider the following:

1: public class Runner
2: {
3:      public void main( String[] args )
4:      {
5:
6:      }
7: }
8: 
9: public class threaded extends Thread
10: {
11:     public boolean run()
12:     {
13:             // stuff
14:     }
15: }
16: 
17: public alsoThreaded implements Runnable
18: {
19:     protected void run( String thread_name )
20:     {
21:             // more stuff
22:     }
23: }
1) There are a few lines which will not compile. What are they and how can you fix them?

________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

2) Write code in Runner's main method which creates 5 threaded objects and 5 alsoThreaded objects.

3) List and define the two different possibilities for thread implementation in virutal machines.

________________________________________________________________
________________________________________________________________
________________________________________________________________

________________________________________________________________
________________________________________________________________
________________________________________________________________

4) One of the beauties of Java is that it is a cross platform language. You, the developer, are guarenteed one type of thread implementation across all virtual machines.

TRUE / FALSE

5) Consider the following:

public class Lala
{
        public static void main( String[] args )
        {
                // ....
                // already created the thread
                // ....
                thisThread.start();
                thisThread.stop();
                thisThread.run();
        }
}

class thisThread extends Thread
{
        public void run()
        {
                System.out.println( "Java Kicks Booty" );
        }
}

Assuming that the user executed java Lala, what will be the output? Explain your answer. warning: tricky

_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________

6) Explain the difference between yield() and suspend().

_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________

7) When a thread calls wait(), it gives up the CPU, but it doesn't give up the lock.

TRUE / FALSE

8) With the call notifyAll(), every thread waiting on the object will be put directly into the running state. With notify(), the developer can choose the particular thread to be put directly into the running state.

There are two things wrong with the statement above. Point them out, and tell why they are wrong.

_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________