/* This program illustrates the difference between a thread object and a synchronization object. */ // a synchronization class: class A { int x = 1; public synchronized void f(int callerid) { System.out.println(callerid+" is calling A.f"); System.out.println("this.x is "+this.x); // get a pointer to the thread that's running this code: T ct = (T)Thread.currentThread(); // note the type down-casting from Thread to T System.out.println("Thread.currentThread().x is "+ ct.x); } } // A // a thread class: class T extends Thread { int x = 2; A a0; int id; public T(int id, A x) {a0=x; this.id=id;} public void run() { try { for (int i=0;i<10;i++) { a0.f(id); Thread.sleep(1000); // 1 second delay } } catch(InterruptedException ie) {System.out.println(ie);} } // run } // T public class whatsthis { public static void main(String[] args) throws InterruptedException { A s = new A(); T t1, t2; t1 = new T(1,s); t2 = new T(2,s); t1.start(); t2.start(); t1.join(); t2.join(); } // main } /* The A object s is just an inert entity. */