java中线程的实现方式
java中线程的实现方式
- 继承Thread类
代码 :
package ThreadImplamen;
import static java.lang.Thread.sleep;
public class threadImp {
public static void main(String[] args) {
Thread thread = new Imp1();
thread.start();
int i;
for (i=0;i<5;i++){
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Imp1 extends Thread{
@Override
public void run() {
int i;
for (i=0;i<5;i++){
System.out.println("继承 Thread类实现的线程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试:
Main!
继承 Thread类实现的线程!
Main!
继承 Thread类实现的线程!
Main!
继承 Thread类实现的线程!
Main!
继承 Thread类实现的线程!
继承 Thread类实现的线程!
Main!
- 实现Runnable接口
代码:
package ThreadImplamen;
import static java.lang.Thread.sleep;
public class ThreadImpl2 {
public static void main(String[] args) {
Runnable run = new RunnableImp();
Thread thread = new Thread(run);
thread.start();
int i;
for (i=0;i<5;i++){
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class RunnableImp implements Runnable{
@Override
public void run() {
int i;
for (i=0;i<5;i++){
System.out.println("实现Runnable接口实现的线程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试:
实现Runnable接口实现的线程!
Main!
Main!
实现Runnable接口实现的线程!
实现Runnable接口实现的线程!
Main!
实现Runnable接口实现的线程!
Main!
Main!
实现Runnable接口实现的线程!
- 实现Callable接口
此方法可接受返回结果
代码:
package ThreadImplamen;
import static java.lang.Thread.sleep;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javax.script.ScriptException;
public class ThreadImpl3 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable callable = new CallableImp();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask);
thread.start();
int i;
for (i = 0; i < 5; i++) {
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(futureTask.get());
}
}
class CallableImp implements Callable {
@Override
public Object call() throws Exception{
int i;
for (i = 0; i < 5; i++) {
System.out.println("实现Callable接口实现的线程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return String.valueOf("这是 你要的返回值");
}
}
测试:
Main!
实现Callable接口实现的线程!
实现Callable接口实现的线程!
Main!
Main!
实现Callable接口实现的线程!
Main!
实现Callable接口实现的线程!
Main!
实现Callable接口实现的线程!
这是 你要的返回值
- 线程池
代码:
package ThreadImplamen;
import static java.lang.Thread.sleep;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadImpl4 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
int pcount = Runtime.getRuntime().availableProcessors();
//最大线程数控制
int maxthreadNum = 5;
ExecutorService executor = new ThreadPoolExecutor(pcount, maxthreadNum, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 2; i++) {
executor.execute(new test1());
}
int i;
for (i = 0; i < 5; i++) {
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class test1 implements Runnable{
@Override
public void run() {
int i;
for (i = 0; i < 5; i++) {
System.out.println("在线程池内!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试:
在线程池内!
在线程池内!
Main!
在线程池内!
在线程池内!
Main!
在线程池内!
在线程池内!
Main!
在线程池内!
在线程池内!
Main!
在线程池内!
Main!
在线程池内!