9.1java多線程的實(shí)現(xiàn)
繼承Tread類
Thread 為Runnable 的子類斑司,子類為多線程操作類,必須覆寫run方法但汞,此方法為線程主體宿刮。正確啟動(dòng)是不可用run()方法,需要調(diào)用start()方法,重復(fù)調(diào)用會(huì)出現(xiàn)異常
package commondy;
class MyTread extends Thread {
private String name;
public MyTread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(name + "運(yùn)行,i=" + i);
}
}
}
class MyTread2 extends Thread {
private String name;
public MyTread2(String name) {
this.name = name;
}
public void run() {
for (int j = 10; j > 0; j--) {
System.out.println(name + "運(yùn)行,i=" + j);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyTread mt1 = new MyTread("線程A");
MyTread2 mt2 = new MyTread2("線程B");
mt1.start();
mt2.start();
}
}
Runnable接口私蕾,方便資源共享僵缺,thread則不能
使用public Tread(Runnable target)或public(Runnable target,String)
package commondy;
class MyThread implements Runnable {
private String name;
public MyThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(name + "運(yùn)行,i=" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread my1 = new MyThread("線程A");
MyThread my2 = new MyThread("線程B");
Thread t1 = new Thread(my1);
Thread t2 = new Thread(my2);
t1.start();
t2.start();
}
}
package commondy;
class MyThread implements Runnable {
private int ticket = 5;
public void run() {
for (int i = 0; i < 10; i++) {
if (ticket > 0) {
System.out.println("賣票:ticket = " + ticket--);
}
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread my = new MyThread();
new Thread(my).start();
new Thread(my).start();
new Thread(my).start();
}
}
package commondy;
class MyThread extends Thread {
private int ticket = 5;
public void run() {
for (int i = 0; i < 10; i++) {
if (ticket > 0) {
System.out.println("賣票:ticket = " + ticket--);
}
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread my = new MyThread();
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my.start();
my1.start();
my2.start();
}
}
9.2 多線程的狀態(tài)
可執(zhí)行狀態(tài)之下,調(diào)用suspend(),sleep(),wait()方法可以進(jìn)入阻塞踩叭,線程調(diào)用stop()或run()方法執(zhí)行結(jié)束進(jìn)入死亡狀態(tài)
9.3線程操作的相關(guān)方法
取得與設(shè)置線程名稱
若未設(shè)置名稱磕潮,系統(tǒng)會(huì)自動(dòng)分配名稱,允許兩個(gè)Thread有相同的名稱
package commondy;
class MyThread implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread my = new MyThread();
new Thread(my).start();
new Thread(my, "線程-A").start();
new Thread(my, "線程-B").start();
new Thread(my).start();
new Thread(my).start();
}
}
主方法也是一個(gè)線程,java程序每次至少啟動(dòng)兩個(gè)線程,main線程與垃圾回收機(jī)制
package commondy;
class MyThread implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread my = new MyThread();
new Thread(my, "線程-A").start();
my.run();
my.run();
}
}
判斷線程是否啟動(dòng)
package commondy;
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt, "線程");
System.out.println("線程開始執(zhí)行之前-->" + t.isAlive());
t.start();
System.out.println("線程開始執(zhí)行之后" + t.isAlive());
for (int i = 0; i < 3; i++) {
System.out.println("main運(yùn)行-->" + i);
}
System.out.println("代碼執(zhí)行之后-->" + t.isAlive());
}
}
線程的強(qiáng)制運(yùn)行
package commondy;
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt, "線程");
t.start();
for (int i = 0; i < 50; i++) {
if (i > 10) {
try {
t.join();
throw new Exception();
} catch (Exception e) {
System.out.println("Main線程運(yùn)行 -->" + i);
}
}
}
}
}
線程的休眠
每次間隔休眠
package commondy;
class MyThread implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(Thread.currentThread().getName() + "運(yùn)行-->" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt);
t.start();
}
}
中斷線程,中斷另一個(gè)
package commondy;
class MyThread implements Runnable {
public void run() {
System.out.println("1.進(jìn)入線程方法");
try {
Thread.sleep(10000);
System.out.println("2.完成休眠");
} catch (Exception e) {
System.out.println("3容贝。休眠被終止");// TODO: handle exception
return;
}
System.out.println("4.run方法正常結(jié)束");
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt, "線程");
t.start();
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
t.interrupt();
}
}
后臺(tái)線程
前臺(tái)有線程自脯,java進(jìn)程則不會(huì)消失,若設(shè)置后臺(tái)線程斤富,則進(jìn)程結(jié)束后膏潮,線程仍可進(jìn)行
package commondy;
class MyThread implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + "在運(yùn)行");
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt, "線程");
t.setDaemon(true);
t.start();
}
}
設(shè)置優(yōu)先級(jí)
package commondy;
class MyThread implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
Thread t1 = new Thread(new MyThread(), "線程A");
Thread t2 = new Thread(new MyThread(), "線程B");
Thread t3 = new Thread(new MyThread(), "線程C");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}