java中线程有几种状态图解
1个回答
2017-06-09 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
1. NEW, 这个最简单了,
static void NEW() {
Thread t = new Thread ();
System. out.println(t.getState());
}
输出NEW
2. RUNNABLE, 也简单, 让一个thread start, 同时代码里面不要sleep或者wait等
private static void RUNNABLE() {
Thread t = new Thread(){
public void run(){
for(int i=0; i<Integer.MAX_VALUE; i++){
System. out.println(i);
}
}
};
t.start();
}
3. BLOCKED, 这个就必须至少两个线程以上, 然后互相等待synchronized 块
private static void BLOCKED() {
final Object lock = new Object();
Runnable run = new Runnable() {
@Override
public void run() {
for(int i=0; i<Integer.MAX_VALUE; i++){
synchronized (lock) {
System. out.println(i);
}
}
}
};
Thread t1 = new Thread(run);
t1.setName( “t1”);
4. WAITING, 这个需要用到生产者消费者模型, 当生产者生产过慢的时候, 消费者就会等待生产者的下一次notify
private static void WAITING() {
final Object lock = new Object();
Thread t1 = new Thread(){
@Override
public void run() {
int i = 0;
while(true ){
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
}
System. out.println(i++);
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
while(true ){
synchronized (lock) {
for(int i = 0; i< 10000000; i++){
System. out.println(i);
}
lock.notifyAll();
}
}
}
};
Thread t2 = new Thread(run);
5. TIMED_WAITING, 这个仅需要在4的基础上, 在wait方法加上一个时间参数进行限制就OK了.
把4中的synchronized 块改成如下就可以了.
synchronized (lock) {
try {
lock.wait(60 * 1000L);
} catch (InterruptedException e) {
}
System. out .println(i++);
}
t2.setName( “t2”);
t1.start();
t2.start();
}
6. TERMINATED, 这个状态只要线程结束了run方法, 就会进入了…
private static void TERMINATED() {
Thread t1 = new Thread();
t1.start();
System. out.println(t1.getState());
try {
Thread. sleep(1000L);
} catch (InterruptedException e) {
}
System. out.println(t1.getState());
}
static void NEW() {
Thread t = new Thread ();
System. out.println(t.getState());
}
输出NEW
2. RUNNABLE, 也简单, 让一个thread start, 同时代码里面不要sleep或者wait等
private static void RUNNABLE() {
Thread t = new Thread(){
public void run(){
for(int i=0; i<Integer.MAX_VALUE; i++){
System. out.println(i);
}
}
};
t.start();
}
3. BLOCKED, 这个就必须至少两个线程以上, 然后互相等待synchronized 块
private static void BLOCKED() {
final Object lock = new Object();
Runnable run = new Runnable() {
@Override
public void run() {
for(int i=0; i<Integer.MAX_VALUE; i++){
synchronized (lock) {
System. out.println(i);
}
}
}
};
Thread t1 = new Thread(run);
t1.setName( “t1”);
4. WAITING, 这个需要用到生产者消费者模型, 当生产者生产过慢的时候, 消费者就会等待生产者的下一次notify
private static void WAITING() {
final Object lock = new Object();
Thread t1 = new Thread(){
@Override
public void run() {
int i = 0;
while(true ){
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
}
System. out.println(i++);
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
while(true ){
synchronized (lock) {
for(int i = 0; i< 10000000; i++){
System. out.println(i);
}
lock.notifyAll();
}
}
}
};
Thread t2 = new Thread(run);
5. TIMED_WAITING, 这个仅需要在4的基础上, 在wait方法加上一个时间参数进行限制就OK了.
把4中的synchronized 块改成如下就可以了.
synchronized (lock) {
try {
lock.wait(60 * 1000L);
} catch (InterruptedException e) {
}
System. out .println(i++);
}
t2.setName( “t2”);
t1.start();
t2.start();
}
6. TERMINATED, 这个状态只要线程结束了run方法, 就会进入了…
private static void TERMINATED() {
Thread t1 = new Thread();
t1.start();
System. out.println(t1.getState());
try {
Thread. sleep(1000L);
} catch (InterruptedException e) {
}
System. out.println(t1.getState());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询