- class Data
- {
- String name;
- String number;
- }
- class Save implements Runnable
- {
- Save(Data d)
- {
- this.d=d;
- }
- public void run()
- {
- int x = 0;
- while (true)
- {
- if (x==0)
- {
- d.name="张三";
- d.number="111111111";
- x++;
- }
- else
- {
- d.name="李四";
- d.number="0000000";
- x--;
- }
- }
- }
- }
- class Get implements Runnable
- {
- Get(Data d)
- {
- this.d=d;
- }
- public void run()
- {
- while (true)
- {
- System.out.println(d.name+"----"+d.number);
- }
- }
- }
- class Running
- {
- public static void main(String[] args)
- {
- Data d = new Data();
- Save s = new Save(d);
- Get g = new Get(d);
- Thread t1 = new Thread(s);
- Thread t2 = new Thread(g);
- t1.start();
- t2.start();
- }
- }
复制代码
|