简体   繁体   English

如何获取带有可运行工具的调用线程的对象?

[英]how to get objects of calling thread with implements runnable?

Hello I have a question with my code. 您好,我的代码有问题。 As you can see below, I have r.getBlink() under OnDraw() method. 如下所示,我在OnDraw()方法下有r.getBlink()。 Is there a function to determine if the "s" thread calls the OnDraw(). 是否有确定“ s”线程是否调用OnDraw()的函数。 I mean I tried this.getClass().getBlink() but it displays as an error. 我的意思是我尝试了this.getClass()。getBlink(),但它显示为错误。 help please. 请帮助。

     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();}
      }
   }
}
Thread.currentThread().get Name();

-This method will give you the name of the currently executing thread object. -此方法将为您提供当前正在执行的线程对象的名称。 I believe this is what you want. 我相信这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM