Exercise 6 Name _______________ __/15
Document last modified:
1. (4 pts) What is one possible sequence of instructions executed for the complete
program?
- public class sequencedThread
- {
- public static void main(String args[]) throws
Exception
- {
- for (int i=1; i<=3; i++)
-
new NumberedThread(i).start();
-
-
System.out.println("main completed");
- }
- }
-
- class NumberedThread extends Thread
- {
- int n;
-
- public NumberedThread(int n)
- { this.n = n;
- }
-
- public void run()
- {
- System.out.println("
[Start Thread " + n + "]");
- for(char ch='a'; ch<'d';
ch++)
-
System.out.println("{Working Thread " + n + " <" + ch+ ">}");
- System.out.println("
[Completed Thread " + n + "]");
- }
- }
|
2. (4 pts) What is reasonable expected output?
3. (1 pts) Are other outputs possible?
4. (4 pts) What is the sequence of instructions executed for the complete
program?
- public class ex7
- {
- public static void main(String args[]) throws
Exception
- { String string = new String("Go IUS");
- for (int i=1; i<=3; i++)
-
new NumberedThread(i, string).start();
-
-
System.out.println("main completed");
- }
- }
-
- class NumberedThread extends Thread
- {
- int n;
- String string;
-
- public NumberedThread(int n, String string)
- { this.n = n;
- this.string =
string;
- }
-
- public void run()
- {
- System.out.println("
[Start Thread " + n + "]");
- synchronized(string)
{
-
for(char i='a'; i<'d'; i++)
-
System.out.println("{Working Thread " + n + " <" + i + ">}");
- }
- System.out.println("
[Completed Thread " + n + "]");
- }
- }
|
5. (2 pts) Rearrange the following output into proper order for the above
program. There are two mistakes.
main completed
[Start Thread 1]
{Working Thread 1 <b>}
{Working Thread 1 <a>}
{Working Thread 1 <c>}
[Start Thread 2]
[Start Thread 3]
[Completed Thread 1]
{Working Thread 2 <a>}
{Working Thread 2 <b>}
{Working Thread 3 <a>}
{Working Thread 2 <c>}
[Completed Thread 2]
{Working Thread 3 <b>}
{Working Thread 3 <c>}
[Completed Thread 3]