9.1java多线程的实现
继承Tread类
Thread 为Runnable 的子类,子类为多线程操作类,必须覆写run方法,此方法为线程主体。正确启动是不可用run()方法,需要调用start()方法,重复调用会出现异常
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 + "运行,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 + "运行,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 + "运行,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 多线程的状态
可执行状态之下,调用suspend(),sleep(),wait()方法可以进入阻塞,线程调用stop()或run()方法执行结束进入死亡状态
9.3线程操作的相关方法
取得与设置线程名称
若未设置名称,系统会自动分配名称,允许两个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();
}
}
主方法也是一个线程,java程序每次至少启动两个线程,main线程与垃圾回收机制
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();
}
}
判断线程是否启动
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("线程开始执行之前-->" + t.isAlive());
t.start();
System.out.println("线程开始执行之后" + t.isAlive());
for (int i = 0; i < 3; i++) {
System.out.println("main运行-->" + i);
}
System.out.println("代码执行之后-->" + t.isAlive());
}
}
线程的强制运行
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线程运行 -->" + 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() + "运行-->" + i);
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt);
t.start();
}
}
中断线程,中断另一个
package commondy;
class MyThread implements Runnable {
public void run() {
System.out.println("1.进入线程方法");
try {
Thread.sleep(10000);
System.out.println("2.完成休眠");
} catch (Exception e) {
System.out.println("3。休眠被终止");// TODO: handle exception
return;
}
System.out.println("4.run方法正常结束");
}
}
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();
}
}
后台线程
前台有线程,java进程则不会消失,若设置后台线程,则进程结束后,线程仍可进行
package commondy;
class MyThread implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + "在运行");
}
}
}
public class demo1 {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t = new Thread(mt, "线程");
t.setDaemon(true);
t.start();
}
}
设置优先级
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();
}
}