用Java创建线程的方式是什么?
有两种方法可以创建新的执行线程。
通过实现可运行的接口
创建线程的一种方法是通过实现Runnable接口来实现-
创建一个类并实现Runnable接口。
重写Runnable接口的 run()方法。
通过将当前类(实现Runnable接口的类)的对象传递给其构造函数来实例化Thread类。
创建Thread对象后,您可以通过调用start()method来启动它,然后依次执行对run()method的调用。
示例
class first implements Runnable {
public void run(){
try {
for(int i=0;i<=20;i+=2) {
Thread.sleep(600);
System.out.println(i);
}
} catch(Exception e) {
}
}
class MyThreadd {
public static void main(String args[]) {
first obj = new first();
Thread thread = new Thread(obj);
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
System.out.println(thread.isAlive());
}
}输出结果
true 0 2 4 6 8 10 12 14 16 18 20
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿