package thread;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
class pub{
private static int x=100;
public int getX(){
return x;
}
public int fix(int y)
{
x=x-y;
return y;
}
}
public class thread1 implements Runnable{
public pub td=new pub();
/**
* @param args
*/
private pub p=new pub();
public static void main(String[] args) {
// TODO Auto-generated method stub
pub p=new pub();
thread1 t=new thread1();
Thread t1=new Thread(t,"THread1");
Thread t2=new Thread(t,"THread2");
t1.start();
t2.start();
}
public void run(){
for(int i=0;i<3;++i)
{
synchronized (this) {
this.fix(30);
try{
Thread.sleep(1);
}catch(Exception e){
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+":当前td对象的值="+td.getX()+"\n");
}
}
}
public int fix(int y)
{
return td.fix(y);
}
}
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
class pub{
private static int x=100;
public int getX(){
return x;
}
public int fix(int y)
{
x=x-y;
return y;
}
}
public class thread1 implements Runnable{
public pub td=new pub();
/**
* @param args
*/
private pub p=new pub();
public static void main(String[] args) {
// TODO Auto-generated method stub
pub p=new pub();
thread1 t=new thread1();
Thread t1=new Thread(t,"THread1");
Thread t2=new Thread(t,"THread2");
t1.start();
t2.start();
}
public void run(){
for(int i=0;i<3;++i)
{
synchronized (this) {
this.fix(30);
try{
Thread.sleep(1);
}catch(Exception e){
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+":当前td对象的值="+td.getX()+"\n");
}
}
}
public int fix(int y)
{
return td.fix(y);
}
}