如何使用implements runnable获取调用线程的对象?

时间:2012-02-28 04:55:16

标签: java android eclipse

您好我的代码有疑问。如下所示,我在OnDraw()方法下有r.getBlink()。是否有一个函数来确定“s”线程是否调用OnDraw()。我的意思是我尝试了this.getClass()。getBlink()但它显示为错误。求助。

     public class main extends Activity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(new BitmapView(this));
          }
    }

    class BitmapView extends View implements OnTouchListener{
          Circle r = new Circle();
          Circle s = new Circle();

          public BitmapView(Context context) {
          super(context);
                  this.setOnTouchListener(this);
          r.starter();
          s.starter();
}

public void onDraw(Canvas canvas){

    Paint paint = new Paint();
    if (r.getBlink()==true){
        paint.setColor(Color.YELLOW);
        r.setBlink(false);
    }
    else{
        paint.setColor(Color.BLACK);
        r.setBlink(true);
    }

    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(r.getX(), r.getY(), 15, paint);
    invalidate();
 }
 class Circle implements Runnable{
    Random rnd=new Random();
    int x=rnd.nextInt(200), y=rnd.nextInt(200);
    boolean blink = false;
    public int getX(){  return this.x;  }
    public int getY(){  return this.y;  }
    public boolean getBlink() { return this.blink;  }
    public void setBlink(boolean b) { this.blink=b;  }

    public void starter(){
       Thread running=new Thread(this);
       running.start();
       x=rnd.nextInt(200);
       y=rnd.nextInt(200);
   }
   public void run(){
      while(true){
         try {
           Thread.sleep(rnd.nextInt(2000));
         } catch (Exception ex) {ex.printStackTrace();}
      }
   }
}

1 个答案:

答案 0 :(得分:-1)

Thread.currentThread().get Name();

- 此方法将为您提供当前正在执行的线程对象的名称。我相信这就是你想要的。