关于线程的困惑 [英] Confusion about Thread

查看:118
本文介绍了关于线程的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以创建接口的引用,但不能创建对象。但是我们如何将新的Runnable()传递给Thread构造函数。根据我的知识是Concert,新的Class_Name()是对象。

We can create a reference of an interface but not object. But how can we pass new Runnable() to the Thread constructor. As per as my knowledge is concert new Class_Name() is object.

Thread t = new Thread(new Runnable(){});


推荐答案

此处使用的技巧称为匿名类。基本上,您正在创建一个新的匿名类的对象,该类实现 Runnable

The trick used here is called anonymous classes. Basically, you are creating a object of a new anonymous class that implements Runnable.

更好的例子是:

Thread t = new Thread(new Runnable(){ 
   @Override
   public void run() {
       // Code here
   }
});

这篇关于关于线程的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆