您的位置:首页 > 编程语言 > Java开发

【JAVA之多线程】2.运行状态+基本方法

2017-02-25 14:52 204 查看
一、运行状态



多个线程时,CPU某一时刻只执行一个线程,其他线程等待被执行,处于临时(阻塞)状态。

某个线程正在运行,突然不想再继续运行,想休息一会,采用sleep/wait,进入冻结状态,等待休息时间到了,或者被唤醒notify,拥有了运行资格,进入阻塞状态,等待被执行。

运行时,使用stop,强行停止线程,结束运行(消亡)。或者run方法内的执行代码执行完毕,该线程也会消亡。

二、基本方法



sleep方法

public class Demo {
public static void main(String[] args) {
T d = new T();
d.start();
try {
Thread.sleep(10000);//该方法在哪个线程里就是sleep哪个线程,主线程睡眠10s
} catch(InterruptedException e) {}
d.interrupt();//中断线程,比较粗暴
//d.stop();比interrupt()还要粗暴,最好不要用,直接让线程死掉
//flag = false;使用这种方法让线程停掉
}
}

class T extends Thread {
//boolean flag = true;
public void run() {
while(true) {//while(flag)
sopl("-----" + new Date() + "-----");

try {
sleep(1000);//每隔1s更新一次时间
} catch(InterruptedException e) {
return;//如果线程被中断,直接结束
}
}
}
}


线程名称

//线程拥有自己默认的名称
//Thread.getName();Thread-编号(从0开始)

public class Demo {
public static void main(String[] args){
T d = new T();
d.start();
for(int i = 0; i < 5; i++) {
System.out.println("Hello World----" + i);
}
}
}

class T extends Thread {
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println(this.getName() + i);
}
}
}
//运行结果:
Hello World----0
Thread-00
Hello World----1
Hello World----2
Hello World----3
Hello World----4
Thread-01
Thread-02
Thread-03
Thread-04

//自定义线程名称:setName/构造方法
public class Text {
public static void main(String[] args){
Demo d = new Demo("1: ");
d.start();
for(int i = 0; i < 5; i++) {
System.out.println("Hello World----" + i);
}
}
}

class Demo extends Thread {
public Demo(String name) {
super(name);
}

public void run() {
for(int i = 0; i < 5; i++) {
System.out.println(this.getName() + i);
//等价于System.out.println(Thread.currentThread().getName() + i);
//Thread.currentThread()返回当前线程对象==this,标准通用方式
}
}
}
//运行结果:
Hello World----0
1: 0
Hello World----1
1: 1
Hello World----2
1: 2
Hello World----3
1: 3
Hello World----4
1: 4


join:临时加入线程执行,直到该线程执行结束

class JoinDemo extends Thread {
public JoinDemo(String name) {
super(name);
}
public void run() {
for(int x = 0; x < 10; x++) {
System.out.println(getName() + "..." + x);
}
}
}

public class Demo {
public static void main(String[] args) throws InterruptedException{
JoinDemo b1 = new JoinDemo("T1");//线程名称为T1
JoinDemo b2 = new JoinDemo("T2");//线程名称为T1
b1.start();
b2.start();//T2不受T1影响
try {
b1.join();//T2和T1正常执行,主线程放弃执行权,直到T1执行结束,主线程才开始继续执行(与T2无关)
} catch (InterruptedException w) {}

//b2.start();//T1执行结束,T2和主线程正常执行
for(int x = 0; x < 10; x++) {
System.out.println("main throw");
}
}
}
//运行结果:
T1...0
T1...1
T2...0
T1...2
T2...1
T1...3
T2...2
T1...4
T2...3
T1...5
T1...6
T1...7
T1...8
T1...9
T2...4
T2...5
main throw
main throw
main throw
main throw
main throw
main throw
main throw
main throw
main throw
main throw
T2...6
T2...7
T2...8
T2...9


yield:让出CPU,让其他线程执行

class JionDemo implements Runnable {
public void run() {
for(int x = 1; x <= 30; x++) {
sopl(Thread.currentThread().getName() + ":" + x);
if((x % 10) == 0) {
Thread.yield();
//强制让正在运行的线程释放执行权,临时释放,稍微能减缓线程的运行,使线程有几乎平均运行的效果
}
}
}
}

public class Demo {
public static void main(String[] args) throws InterruptedException{
JionDemo b = new JionDemo();//同一个线程类对象可以启两个线程
Thread t1 = new Thread(b);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}


setPriority(int newPriority):更改线程的优先级(共10级:1-10)

设置不设置不明显,依旧互相抢夺执行权,只是抢的厉害或弱一些

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 多线程 线程
相关文章推荐