本帖最后由 攀攀 于 2014-6-28 11:05 编辑
- <DIV class=blockcode>
- <BLOCKQUOTE>
- <BLOCKQUOTE>
- <DIV class=blockcode>
- <BLOCKQUOTE>
- class Rec
- {
- private String name;
- private String sex;
- private static Rec r = new Rec();
- private Rec(){}
- public static Rec getInstance()
- {
- return r;
- }
- public String getName()
- {
- return name;
- }
- public String getSex()
- {
- return sex;
- }
- public void set(String name , String sex)
- {
- this.name = name;
- this.sex = sex;
- }
- }
- class Input implements Runnable
- {
- Rec.getInstance();
- public void run ()
- {
- int x = 0;
- while (true)
- {
- if (x==0)
- {
- r.set("mike" ,"man");
- }
- else
- {
- r.set("张三","男");
- }
- x = (x+1)%2;
- }
- }
- }
- class Output implements Runnable
- {
- Rec.getInstance();
- public void run ()
- {
- while (true)
- {
- System.out.println(r.getName()+"......"+r.getSex());
- }
- }
- }
- class OutputDemo
- {
- public static void main(String[] args)
- {
- new Thread (new Input()).start();
- new Thread (new Output()).start();
- }
- }
复制代码
|
|