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]
上面代码的运行结果如下:
如对本文有疑问, 点击进行留言回复!!
springmvc之ResponseBody响应json数据遇到的错误及解决
uni-app 后台升级 静默升级 uniapp 后台更新 静默更新 在线升级
SpringBoot多Module启动报错Could not transfer metadata
Hibernate项目报错:Cannot call sendError() after the response has been committed
网友评论