java多线程有两种方式,一种是继承Thread类,一种是实现Runable接口
1、继承Thread类:
class threadTest extends Thread {
publicthreadTest() {
}
publicthreadTest(String name) {
this.name = name;
}
publicvoidrun() {
for(inti =0; i <5; i++) {
System.out.println(name +"运行 "+ i);
}
}
publicstaticvoidmain(String[] args) {
threadTest t1=newthreadTest("A");
threadTestt2=newthreadTest("B");
t1.start();
t2.start();
}
privateString name;
}
这里的的start()方法是运行线程中的run()方法
2、实现Runable接口
class runableTest implements Runnable {
publicrunableTest() {
}
publicrunableTest(String name) {
this.name = name;
}
publicvoidrun() {
for(inti =0; i <5; i++) {
System.out.println(name +"运行 "+ i);
}
}
publicstaticvoidmain(String[] args) {
runableTestr1=newrunableTest("线程A");
Thread demo= newThread(r1);
runableTest r2=newrunableTest("线程B");
Thread demo1=newThread(r2);
demo.start();
demo1.start();
}
privateString name;
}
查看Thread源码,可以发现Thread其实也是实现Runable接口,不过Thread和Runable的区别:
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
对于有共享资源的时候,使用Runable接口最好,如车票信息
class MyThread implements Runnable{
privateintticket =5; //5张票
publicvoidrun() {
for(inti=0; i<=20; i++) {
if(this.ticket >0) {
System.out.println(Thread.currentThread().getName()+"正在卖票"+this.ticket--);
}
}
}
}
public class lzwCode {
publicstaticvoidmain(String [] args) {
MyThread my =newMyThread();
newThread(my,"1号窗口").start();
newThread(my,"2号窗口").start();
newThread(my,"3号窗口").start();
}
}
总结一下:
实现Runnable接口比继承Thread类所具有的优势:
1):适合多个相同的程序代码的线程去处理同一个资源
2):可以避免java中的单继承的限制
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
注意:main方法其实也是一个线程。在java中所以的线程都是同时启动的,至于什么时候,哪个先执行,完全看谁先得到CPU的资源。
在java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。因为每当使用java命令执行一个类的时候,实际上都会启动一个JVM,每一个jVM实习在就是在操作系统中启动了一个进程。
除此之外还有线程的一些方法
线程是否启动:isAlive()
线程强制执行:join()
线程中断:interrupt()
线程的优先级:setPriority(
int lev
)其取值范围是1 (Thread.MIN_PRIORITY) - 10 (Thread.MAX_PRIORITY)注意:并不是优先级高的就一定执行,要根据cpu的资源而定。另外,主线程的优先级是5
线程休眠:sleep(int time) time是休眠时间, 一般为毫秒
线程的让步:yield()
下面说一下sleep()和yield()的区别:
1)sleep()方法给其他线程运行机会时不考虑线程的优先级,因此会给低优先级的线程以运行的机会;yield()方法只会给相同优先级或更高优先级的线程以运行的机会;
2)线程执行sleep()方法后转入阻塞(blocked)状态,而执行yield()方法后转入就绪(ready)状态;
3)sleep()方法声明抛出InterruptedException,而yield()方法没有声明任何异常;
4)sleep()方法比yield()方法(跟操作系统CPU调度相关)具有更好的可移植性.