Thread類(須有start調(diào)用run才可啟動(dòng)線程)
public void start()
使該線程開始執(zhí)行;Java 虛擬機(jī)調(diào)用該線程的 run 方法。
結(jié)果是兩個(gè)線程并發(fā)地運(yùn)行;當(dāng)前線程(從調(diào)用返回給 start 方法)和另一個(gè)線程(執(zhí)行其 run 方法)。多次啟動(dòng)一個(gè)線程是非法的。特別是當(dāng)線程已經(jīng)結(jié)束執(zhí)行后,不能再重新啟動(dòng)。
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
}
public void run(){
for(int x=0;x<200;x++){
System.out.println(name+"----->"+x);
}
}
}
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread m1 = new MyThread("Java");
MyThread m2 = new MyThread("Javb");
MyThread m3 = new MyThread("Javc");
m1.run();
m2.run();
m3.run();
}
}
多線程執(zhí)行
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
}
public void run(){
for(int x=0;x<200;x++){
System.out.println(name+"----->"+x);
}
}
}
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread m1 = new MyThread("Java");
MyThread m2 = new MyThread("Javb");
MyThread m3 = new MyThread("Javc");
m1.start();
m2.start();
m3.start();
}
}
Runnable接口(由于單繼承局限)
public class Thread extends Object implements Runnable
class MyThread implements Runnable{
private String name;
public MyThread(String name){
this.name = name;
}
public void run(){
for(int x=0;x<200;x++){
System.out.println(name+"----->"+x);
}
}
}
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread m1 = new MyThread("Java");
MyThread m2 = new MyThread("Javb");
MyThread m3 = new MyThread("Javc");
new Thread(m1).start();
new Thread(m2).start();
new Thread(m3).start();
}
}
用Runnable實(shí)現(xiàn)主體類,用Thread啟動(dòng)線程
Callable接口
暫無
線程的命名與取得
構(gòu)造方法:Thread(Runnable target, String name)
public static Thread currentThread()此方法取得當(dāng)前對(duì)象
package Demo;
//如果啟動(dòng)前未設(shè)置名字,則會(huì)自動(dòng)編號(hào)命名
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
}
public class CollableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
------------------------------------
Thread-0
Thread-1
Thread-2
設(shè)置名字:public final void setName(String name)
package Demo;
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
}
public class CollableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
new Thread(mt,"線程A").start();
new Thread(mt).start();
new Thread(mt,"線程B").start();
new Thread(mt).start();
}
}
取得名字:public final String getName()
package Demo;
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
}
public class CollableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
new Thread(mt,"線程A").start();
mt.run();
}
}
--------------------------
main
線程A
主方法就是一個(gè)線程(main線程),在子方法上創(chuàng)建的線程為子線程;main只是進(jìn)程上的一個(gè)子線程。
線程的休眠
public static void sleep(long millis,int nanos) throws InterruptedException
package Demo;
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int x=0;x<1000;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x ="+x);
}
}
}
public class CollableDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
new Thread(mt,"線程A").start();
}
}
如果設(shè)置了多個(gè)線程對(duì)象,則一起進(jìn)入run()方法
package Demo;
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int x=0;x<1000;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x ="+x);
}
}
}
public class CollableDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
new Thread(mt,"線程A").start();
new Thread(mt,"線程B").start();
new Thread(mt,"線程C").start();
new Thread(mt,"線程D").start();
new Thread(mt,"線程E").start();
}
}
------------------------------------
線程B,x =0
線程A,x =0
線程E,x =0
線程D,x =0
線程C,x =0
線程C,x =1
線程A,x =1
線程B,x =1
線程E,x =1
線程D,x =1
線程E,x =2
線程C,x =2
線程A,x =2
線程D,x =2
線程B,x =2
線程E,x =3
線程A,x =3
線程C,x =3
線程D,x =3
線程B,x =3
線程D,x =4
線程E,x =4
線程C,x =4
線程A,x =4
線程B,x =4
線程優(yōu)先級(jí)
設(shè)置優(yōu)先級(jí):public final void setPriority(int newPriority)
取得優(yōu)先級(jí):public final int getPriority()
使用int返回,有三個(gè)優(yōu)先級(jí)
最高:public static final int MAX_PRIORITY 10
中等:public static final int NORM_PRIORITY 5
最低:public static final int MIN_PRIORITY 1
package Demo;
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int x=0;x<20;x++){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x ="+x);
}
}
}
public class CollableDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Mythread mt = new Mythread();
Thread t1 = new Thread(mt,"線程A");
Thread t2 = new Thread(mt,"線程B");
Thread t3 = new Thread(mt,"線程C");
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}