1.继承Thread
Java
public class java_thread extends Thread{
public static void main(String args[])
{
(new java_thread()).run();
System.out.println("main thread run ");
}
public synchronized void run()
{
System.out.println("sub thread run ");
}
}
2.实现Runnable接口
Java
public class java_thread implements Runnable{
public static void main(String args[])
{
(new Thread(new java_thread())).start();
System.out.println("main thread run ");
}
public void run()
{
System.out.println("sub thread run ");
}
}
3.直接在函数体使用
void java_thread()
{
Thread t = new Thread(new Runnable(){
public void run(){
mSoundPoolMap.put(index, mSoundPool.load(filePath, index));
getThis().LoadMediaComplete();
}});
t.start();
}
1,继承自Thread类,代码逻辑写在子线程中,需要重写run()方法,主线程里start()就可以了
package com.myjava.thread;
public class ExtendsThread extends Thread{
private final static int THREAD_NUM = 5;
public static void main(String[] args){
for (int i = 0; i <THREAD_NUM; i++) {
new ExtendsThread("thread"+i).start();
}
}
public ExtendsThread(String name){
super(name);
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < this.THREAD_NUM; i++) {
System.out.println(this.getName()+i);
}
}
}
运行结果:
thread00
thread01
thread02
thread03
thread04
thread20
thread21
thread22
thread23
thread24
thread40
thread41
thread42
thread43
thread44
thread10
thread11
thread12
thread13
thread14
thread30
thread31
thread32
thread33
thread34
2,实现Runnable接口
package com.myjava.thread;
public class ImplRunnable implements Runnable {
private static final int THREAD_NUM = 5;
@Override
public void run() {
for (int i = 0; i < THREAD_NUM; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int j = 0; j < THREAD_NUM; j++) {
ImplRunnable implRunnable= new ImplRunnable();
new Thread(implRunnable,"thread"+j).start();
}
}
}
运行结果
thread10
thread11
thread12
thread13
thread14
thread30
thread31
thread32
thread33
thread34
thread00
thread01
thread02
thread03
thread04
thread20
thread21
thread22
thread23
thread24
thread40
thread41
thread42
thread43
thread44
3,通过线程池
package com.myjava.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
private static int POOL_NUM = 10;
public static void main(String[] agrs){
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < POOL_NUM; i++) {
RunnableThread thread = new RunnableThread();
executorService.execute(thread);
}
}
}
class RunnableThread implements Runnable{
private int THREAD_NUM = 10;
public void run() {
for (int i = 0; i <THREAD_NUM; i++) {
System.out.println("线程"+Thread.currentThread()+i);
}
}
}
-----
线程Thread[pool-1-thread-2,5,main]0
线程Thread[pool-1-thread-2,5,main]1
线程Thread[pool-1-thread-2,5,main]2
线程Thread[pool-1-thread-2,5,main]3
线程Thread[pool-1-thread-2,5,main]4
线程Thread[pool-1-thread-2,5,main]5
线程Thread[pool-1-thread-2,5,main]6
线程Thread[pool-1-thread-2,5,main]7
线程Thread[pool-1-thread-2,5,main]8
线程Thread[pool-1-thread-2,5,main]9
线程Thread[pool-1-thread-4,5,main]0
线程Thread[pool-1-thread-4,5,main]1
线程Thread[pool-1-thread-4,5,main]2
线程Thread[pool-1-thread-4,5,main]3
线程Thread[pool-1-thread-4,5,main]4
线程Thread[pool-1-thread-4,5,main]5
线程Thread[pool-1-thread-4,5,main]6
线程Thread[pool-1-thread-4,5,main]7
线程Thread[pool-1-thread-4,5,main]8
线程Thread[pool-1-thread-4,5,main]9
线程Thread[pool-1-thread-4,5,main]0
线程Thread[pool-1-thread-4,5,main]1
线程Thread[pool-1-thread-4,5,main]2
线程Thread[pool-1-thread-4,5,main]3
线程Thread[pool-1-thread-4,5,main]4
线程Thread[pool-1-thread-4,5,main]5
线程Thread[pool-1-thread-4,5,main]6
线程Thread[pool-1-thread-4,5,main]7
线程Thread[pool-1-thread-4,5,main]8
线程Thread[pool-1-thread-4,5,main]9
线程Thread[pool-1-thread-1,5,main]0
线程Thread[pool-1-thread-1,5,main]1
线程Thread[pool-1-thread-1,5,main]2
线程Thread[pool-1-thread-1,5,main]3
线程Thread[pool-1-thread-1,5,main]4
线程Thread[pool-1-thread-1,5,main]5
线程Thread[pool-1-thread-1,5,main]6
线程Thread[pool-1-thread-1,5,main]7
线程Thread[pool-1-thread-1,5,main]8
线程Thread[pool-1-thread-1,5,main]9
线程Thread[pool-1-thread-5,5,main]0
线程Thread[pool-1-thread-5,5,main]1
线程Thread[pool-1-thread-5,5,main]2
线程Thread[pool-1-thread-5,5,main]3
线程Thread[pool-1-thread-5,5,main]4
线程Thread[pool-1-thread-5,5,main]5
线程Thread[pool-1-thread-5,5,main]6
线程Thread[pool-1-thread-5,5,main]7
线程Thread[pool-1-thread-5,5,main]8
线程Thread[pool-1-thread-5,5,main]9
线程Thread[pool-1-thread-3,5,main]0
线程Thread[pool-1-thread-3,5,main]1
线程Thread[pool-1-thread-3,5,main]2
线程Thread[pool-1-thread-3,5,main]3
线程Thread[pool-1-thread-3,5,main]4
线程Thread[pool-1-thread-3,5,main]5
线程Thread[pool-1-thread-3,5,main]6
线程Thread[pool-1-thread-3,5,main]7
线程Thread[pool-1-thread-3,5,main]8
线程Thread[pool-1-thread-3,5,main]9
线程Thread[pool-1-thread-2,5,main]0
线程Thread[pool-1-thread-2,5,main]1
线程Thread[pool-1-thread-2,5,main]2
线程Thread[pool-1-thread-2,5,main]3
线程Thread[pool-1-thread-2,5,main]4
线程Thread[pool-1-thread-2,5,main]5
线程Thread[pool-1-thread-2,5,main]6
线程Thread[pool-1-thread-2,5,main]7
线程Thread[pool-1-thread-2,5,main]8
线程Thread[pool-1-thread-2,5,main]9
线程Thread[pool-1-thread-2,5,main]0
线程Thread[pool-1-thread-2,5,main]1
线程Thread[pool-1-thread-2,5,main]2
线程Thread[pool-1-thread-2,5,main]3
线程Thread[pool-1-thread-2,5,main]4
线程Thread[pool-1-thread-2,5,main]5
线程Thread[pool-1-thread-2,5,main]6
线程Thread[pool-1-thread-2,5,main]7
线程Thread[pool-1-thread-2,5,main]8
线程Thread[pool-1-thread-2,5,main]9
线程Thread[pool-1-thread-2,5,main]0
线程Thread[pool-1-thread-2,5,main]1
线程Thread[pool-1-thread-2,5,main]2
线程Thread[pool-1-thread-2,5,main]3
线程Thread[pool-1-thread-2,5,main]4
线程Thread[pool-1-thread-2,5,main]5
线程Thread[pool-1-thread-2,5,main]6
线程Thread[pool-1-thread-2,5,main]7
线程Thread[pool-1-thread-2,5,main]8
线程Thread[pool-1-thread-2,5,main]9
线程Thread[pool-1-thread-2,5,main]0
线程Thread[pool-1-thread-2,5,main]1
线程Thread[pool-1-thread-2,5,main]2
线程Thread[pool-1-thread-2,5,main]3
线程Thread[pool-1-thread-2,5,main]4
线程Thread[pool-1-thread-2,5,main]5
线程Thread[pool-1-thread-2,5,main]6
线程Thread[pool-1-thread-2,5,main]7
线程Thread[pool-1-thread-2,5,main]8
线程Thread[pool-1-thread-2,5,main]9
4.比较:
实现Runnable接口优势:
1)适合多个相同的程序代码的线程去处理同一个资源
2)可以避免java中的单继承的限制
3)增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
继承Thread类优势:
1)可以将线程类抽象出来,当需要使用抽象工厂模式设计时。
2)多线程同步
在函数体使用优势
1)无需继承thread或者实现Runnable,缩小作用域。