当前位置: 移动技术网 > IT编程>开发语言>Java > java多线程编程之使用runnable接口创建线程

java多线程编程之使用runnable接口创建线程

2019年07月22日  | 移动技术网IT编程  | 我要评论

1.将实现runnable接口的类实例化。

2.建立一个thread对象,并将第一步实例化后的对象作为参数传入thread类的构造方法。

最后通过thread类的start方法建立线程。
下面的代码演示了如何使用runnable接口来创建线程:

package mythread;
public class myrunnable implements runnable
{
 public void run()
 {
  system.out.println(thread.currentthread().getname());
 }
 public static void main(string[] args)
 {
  myrunnable t1 = new myrunnable();
  myrunnable t2 = new myrunnable();
  thread thread1 = new thread(t1, "mythread1");
  thread thread2 = new thread(t2);
  thread2.setname("mythread2");
  thread1.start();
  thread2.start();
 }
}
[/code]
上面代码的运行结果如下:

复制代码 代码如下:

mythread1
mythread2

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网