CS 4773 Object Oriented Systems
The Java Language (continued)
Class Modifiers
abstract
Contains abstract methods (methods without implementation),
or is otherwise not directly implemented.
final
Cannot be a subclassed.
All methods are also final
meaning that they cannot be overridden.
public
May be used by code that is outside of the class package.
Only one public class is allowed per file, and the file must be
named by that class.
private
Can only be used within a file
< empty >
If neither public or private is given,
this class is accessible within the current class package only.
Method and Field Modifiers
public
Can be accessed by methods from outside of its class.
protected
Can only be accessed by a subclass.
private
Can only be accessed by methods in this class
< empty >
If none of public, private, or protected is
given, it can be accessed only be methods in the same package.
final
The method cannot be overridden or the variable cannot be
changed.
static
The method is shared by all instances of this class and is invoked
by < Class >.method.
synchronized
The method will lock the object on entry and unlock it on exit.
If the object was already locked, the method waits until the lock is released
before executing.
native
The method is implemented by a stub written in another language.
Threads
There are two main ways to use threads in Java:
- Create an object with a class which extends the class Thread
- The constructor initializes the thread.
- When the thread's start method is called, the thread's
run method executes concurrently with other threads.
- Create an object of type Thread.
- Pass an object from a class which implements Runnable.
- When the thread is started, the run method of that
runnable object is executed.
- Often, the runnable is the same object that creates the thread.
Ping Pong Application
Code for the PingPong programs can be found in
/usr/local/courses/cs4773/spring97/threadtests
PingPong.java
package threadtests;
public class PingPong extends Thread {
String word; // what word to print
int delay; // how long to pause
int count; // number of iterations
PingPong(String What, int Time, int number) {
word = What;
delay = Time;
count = number;
setName(What);
}
public void run() {
try {
for(int i=0;i < count;i++) {
System.out.println(i+": "+word);
sleep(delay); // wait until next time
}
} catch (InterruptedException e) {
return; // end this thread
}
}
}
test1.java
package threadtests;
class test1{
public static void main (String[] args){
PingPong ping;
PingPong pong;
ping = new PingPong("ping", 1000, 20);
pong = new PingPong("PONG", 3000, 8);
ping.start();
pong.start();
}
}
Ping Pong Output
0: ping
0: PONG
1: ping
2: ping
1: PONG
3: ping
4: ping
5: ping
2: PONG
6: ping
7: ping
8: ping
3: PONG
9: ping
10: ping
4: PONG
11: ping
12: ping
13: ping
5: PONG
14: ping
15: ping
16: ping
6: PONG
17: ping
18: ping
7: PONG
19: ping
8: PONG
9: PONG
10: PONG
11: PONG
12: PONG
Using join
Join suspends the caller until the thread has completed.
package threadtests;
class test2{
public static void show_threads(String msg) {
Thread[] tlist = new Thread[50];
int count;
count = Thread.enumerate(tlist);
System.out.println(msg + " Number of threads: "+count);
for (int i=0;i < count;i++)
System.out.println(i+": "+tlist[i]);
}
public static void main (String[] args){
PingPong ping;
PingPong pong;
show_threads("Start of main");
ping = new PingPong("ping", 1000, 20);
show_threads("ping created");
pong = new PingPong("PONG", 3000, 8);
show_threads("pong created");
ping.start();
pong.start();
try {ping.join();} catch(InterruptedException e) {}
show_threads("ping joined");
try {pong.join();} catch(InterruptedException e) {}
show_threads("pong joined");
}
}
Join Ping Pong Output
Start of main Number of threads: 1
0: Thread[main,5,main]
ping created Number of threads: 2
0: Thread[main,5,main]
1: Thread[ping,5,main]
pong created Number of threads: 3
0: Thread[main,5,main]
1: Thread[ping,5,main]
2: Thread[PONG,5,main]
0: ping
0: PONG
1: ping
2: ping
1: PONG
3: ping
4: ping
5: ping
2: PONG
6: ping
7: ping
8: ping
3: PONG
9: ping
10: ping
11: ping
4: PONG
12: ping
13: ping
14: ping
5: PONG
15: ping
16: ping
17: ping
6: PONG
18: ping
19: ping
ping joined Number of threads: 2
0: Thread[main,5,main]
1: Thread[PONG,5,main]
7: PONG
pong joined Number of threads: 1
0: Thread[main,5,main]