
在Java中,我们最常实现多线程的方法有两种java多线程用哪些实现,一种是子类化线程类,另一种是实现Runable接口;

我们可以编写一个类来继承Thread类,然后在其中实现run方法,最后调用start方法来启动线程.

public class t {
public static void main(final String[] args) {
test one=new test();
test two=new test();
one.start();
two.start();
}
}
class test extends Thread{
@Override
public void run() {
for(int i=0;i<100;i++)
{
System.out.println(i);
}
}
}

另一种实现方法是编写一个类来实现Runnable接口,然后将其“扔”到Thread对象中,并使用该对象的start方法启动线程;

public class t {
public static void main(final String[] args) {
test one=new test();
Thread t1=new Thread(one);
Thread t2=new Thread(one);
t1.start();
t2.start();
}
}
class test implements Runnable{
@Override
public void run() {
for(int i=0;i<100;i++)
{
System.out.println(i);
}
}
}
在特定用途中java多线程用哪些实现,建议您使用第二种方法,即实现Runnable接口的方法,因为该方法更加灵活并且可以实现多个线程来处理同一对象.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-288986-1.html
图片上的虫明显是未加工过的活虫
再横起来也不迟