TwoObjects.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- public class TwoObjects extends Object {
- private String objID;
- public TwoObjects(String objID) {
- this.objID = objID;
- }
- public synchronized void doStuff(int val) {
- print("entering doStuff()");
- int num = val * 2 + objID.length();
- print("in doStuff() - local variable num=" + num);
- // slow things down to make observations
- try { Thread.sleep(2000); } catch ( InterruptedException x ) { }
- print("leaving doStuff()");
- }
- public void print(String msg) {
- threadPrint("objID=" + objID + " - " + msg);
- }
- public static void threadPrint(String msg) {
- String threadName = Thread.currentThread().getName();
- System.out.println(threadName + ": " + msg);
- }
- public static void main(String[] args) {
- final TwoObjects obj1 = new TwoObjects("obj1");
- final TwoObjects obj2 = new TwoObjects("obj2");
- Runnable runA = new Runnable() {
- public void run() {
- obj1.doStuff(3);
- }
- };
- Thread threadA = new Thread(runA, "threadA");
- threadA.start();
- try { Thread.sleep(200); } catch ( InterruptedException x ) { }
- Runnable runB = new Runnable() {
- public void run() {
- obj2.doStuff(7);
- }
- };
- Thread threadB = new Thread(runB, "threadB");
- threadB.start();
- }
- }