Java中创建线程主要有三种方式:
①继承thread重写run,调用start
package example.testForJava;
public class threadTest extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName()+" "+i);
}
}
public static void main(String[] args) {
threadTest threadTest1=new threadTest();
threadTest threadTest2=new threadTest();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
if (i==10){
new Thread(threadTest1,"threadTest example1").start();
new Thread(threadTest2,"threadTest example2").start();
}
}
}
}
②实现runnable接口,重写run方法,创建当前实例,new一个thread对象参数为创建的实例,调用start方法
package example.testForJava;
public class runnableTest implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 20) {
runnableTest runnableTest = new runnableTest();
new Thread(runnableTest, "first thread").start();
new Thread(runnableTest, "second thread").start();
}
}
}
}
③实现callable接口,重写call方法,创建当前实例,传入futureTask(),将future作为参数传入创建new thread,执行。最后调用future的get方法得到一个返回值
package example.testForJava;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class callableThreadTest implements Callable<Integer> {
public static void main(String[] args) {
callableThreadTest callableThreadTest = new callableThreadTest();
FutureTask<Integer> futureTask = new FutureTask<>(callableThreadTest);
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " 的循环变量的i的值" + i);
if (i == 20) {
new Thread(futureTask, "futureTask thread").start();
}
}
try {
System.out.println("子 thread return " + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
@Override
public Integer call() throws Exception {
int i = 0;
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
return i;
}
}
总结:常用后两种,因为没有继承类,所以可以继承其他类,多个线程共享一个target,用多个相同的线程处理同一份资源。比如买票多窗口,thread就是每个窗口都卖那么多的票,实现接口就是所有窗口共同去卖这么多的票
//发现每一个thread都卖出了十张,这不是我们需要的
package example.testForJava;
public class threadTest extends Thread {
private int ticket = 10;
@Override
public void run() {
for (int i = 0; i < 50; i++) {
if (this.ticket>0) {
System.out.println(this.getName()+"卖出第"+(10-this.ticket-- + 1)+"票");
}
}
}
public static void main(String[] args) {
threadTest threadTest1 = new threadTest();
threadTest threadTest2 = new threadTest();
threadTest threadTest3 = new threadTest();
new Thread(threadTest1, "thread1").start();
new Thread(threadTest2, "thread2").start();
new Thread(threadTest3, "thread3").start();
}
}
//三个线程共同实现卖10张
package example.testForJava;
public class runnableTest implements Runnable {
private int ticket = 10;
@Override
public void run() {
for (int i = 0; i < 50; i++) {
if (this.ticket > 0) {
System.out.println(Thread.currentThread().getName() + "卖出第" + (10 - this.ticket-- + 1) + "票");
}
}
}
public static void main(String[] args) {
runnableTest runnableTest = new runnableTest();
new Thread(runnableTest, "first thread").start();
new Thread(runnableTest, "second thread").start();
new Thread(runnableTest, "third thread").start();
}
}