本文实例为大家分享了java启动线程的方法,供大家参考,具体内容如下
1.继承thread
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接口
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(); }
4.比较:
实现runnable接口优势:
1)适合多个相同的程序代码的线程去处理同一个资源
2)可以避免java中的单继承的限制
3)增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
继承thread类优势:
1)可以将线程类抽象出来,当需要使用抽象工厂模式设计时。
2)多线程同步
在函数体使用优势
1)无需继承thread或者实现runnable,缩小作用域。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Unity UGUI 之 实现按钮 Button 长按和双击的功能效果
[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets
网友评论