java如何让八个线程交替推行?

c2.await();

线程的2种完成和线程的轮番实践,线程2种交替试行

学了线程,收获累累,记录下了吧.

一、线程的显要三种完毕方式。

1.继承Thread类,重写run()方法  

   main方法中创设子类,援用调用start()方法

实譬喻下:

//继承Thread类,重写run()方法      

public class ThreadOne extends Thread {

public void run() {
for (int i = 1; i <=100; i++) {
System.out.println(this.getName()+”:”+i);
}
}

public static void main(String[] args) {
//创建Thread对象
ThreadOne threadOne = new ThreadOne();
//调用start()
threadOne.start();

}

}

2.实现Runnable()接口,实现run()方法。

 mian方法中 实例该类,实例Thread类并保有该类的援用Thread的征引调用start().

实例:

//实现Runnable()接口,实现run()方法

public class MyThread implements Runnable {
public void run() {
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName()+”放入一个苹果:”+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

public static void main(String[] args) {

//创建MyThread类
MyThread myThread =new MyThread();

//创建Thread,并持有MyThread的应用
Thread thread = new Thread(myThread);

//调用start()
thread.start();
}
}

二.规律交替实施线程标题

比方说难点:完毕七个线程能够交替每隔1分钟打字与印刷一遍时间,每种线程打字与印刷5次后得了线程

结果要求:

Thread-0 Sun Feb 28 11:21:52 CST 2016;
Thread-1 Sun Feb 28 11:21:53 CST 2016
Thread-0 Sun Feb 28 11:21:54 CST 2016
Thread-1 Sun Feb 28 11:21:55 CST 2016
Thread-0 Sun Feb 28 11:21:56 CST 2016
Thread-1 Sun Feb 28 11:21:57 CST 2016
Thread-0 Sun Feb 28 11:21:58 CST 2016
Thread-1 Sun Feb 28 11:21:59 CST 2016
Thread-0 Sun Feb 28 11:22:00 CST 2016
Thread-1 Sun Feb 28 11:22:01 CST 2016

难点浅析:

第一要
完结2个线程:功效都是创设时间;其次需求交替推行,就无法不加锁synchronized;这一个都以能够领略的

尤为重要的是其一锁怎么决定,用怎么样决定,采取方法为:定义一个static状态值
state ,取值为1,2;

当state=1的时候,施行线程1,线程2等待,实践实现后将state=2,并调用notifyAll()
或然notify()方法;

当state=2的时候,实践线程2,线程1等待,推行实现后将state=1,并调用notifyAll()
也许notify()方法;

末尾就剩调整5次的难题了,各自定义2个static int 变量
利用while调节呗,各线程推行一遍,相应的变量值加1到5竣事

代码:

public class ThreadDate{
//状态值 调整 实践哪个程序用
private static int state =1;
//调节线程一举行的次数
private static int num1 =1;
//调节线程2施行的次数
private static int num2 =1;
public static void main(String[] args) {
//用该类作为锁
final ThreadDate t= new ThreadDate();
//创造第3个线程,并调用start()方法
new Thread(new Runnable() {
public void run() {
//用while num1调控实践次数
while(num1<=5){
//加锁
synchronized (t) {
//首先应状态值判别是还是不是该执行
if(state!=1){
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+” “+new Date());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//试行完成,状态值改换
state =2;
t.notifyAll();
}
num1++;
}
}
}).start();;

//创设第2个线程,并调用start()方法
new Thread(new Runnable() {
public void run() {
while(num2<=5){
synchronized (t) {
if(state!=2){
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+” “+new Date());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

state =1;
t.notifyAll();
}
num2++;
}
}
}).start();
}
}

 

http://www.bkjia.com/Javabc/1103520.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1103520.htmlTechArticle线程的2种实现和线程的交替执行,线程2种交替执行
学了线程,收获良多,记录下了吧. 一、线程的注重二种达成情势。
1.承接Thread类,重…

public class ThreadTurnTest2 {

pool-1-thread-2___4

m;

pool-1-thread-2___10

private void m0() throws InterruptedException {

for (int i = 0; i < LIMIT; i += 2) {

* @param i

package org.stathry.jdkdeep.concurrent;

case 2:

t0___8

* 线程间通讯-基于lock的condition

exec.submit(o.createTask;

System.out.println(Thread.currentThread().getName() + “___” + i);

import java.util.concurrent.Executors;

c0.await();

}

}, “t0”).start();

t1___7

}

}

t1___9

private void m(final int n) throws InterruptedException {

private int status = 0;

System.out.println(Thread.currentThread().getName() + “___” + i);

import java.util.concurrent.TimeUnit;

System.out.println(Thread.currentThread().getName() + “___” + i);

*/

exec.submit(o.createTask;

c1.await();

case 1:

/**

}

try {

pool-1-thread-3___5

}, “t1”).start();

import java.util.concurrent.locks.Condition;

pool-1-thread-1___6

}

e.printStackTrace();

pool-1-thread-1___0

public class ThreadTurnTest1 {

* @date 2017年12月21日

status = 2;

ExecutorService exec = new ThreadPoolExecutor(3, 30, 60,
TimeUnit.SECONDS, new LinkedBlockingQueue,

if (status != 2) {

return new Runnable() {

t1___1

看完上面二种方法你学会了吗?

break;

new Thread(new Runnable() {

pool-1-thread-3___14

lock.wait();

private Runnable createTask(final int i) {

}

t0___6

public static void main(String[] args) throws InterruptedException {

pool-1-thread-2___13

private Lock lock = new ReentrantLock();

import java.util.concurrent.LinkedBlockingQueue;

switch {

}

e.printStackTrace();

c2.signal();

}

lock.wait();

}

* @param name

lock.lock();

pool-1-thread-2___7

};

System.out.println(Thread.currentThread().getName() + “___” + i);

c0.signal();

}

实际那三种格局思路都以临近的,大约能够分三步:

pool-1-thread-2___1

break;

}

default:

import java.util.concurrent.ThreadPoolExecutor;

实行结果如下:

* 线程间通信-基于Object的wait/notify

}

lock.unlock();

public void run() {

break;

public void run() {

Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy;

new Thread(new Runnable() {

package org.stathry.jdkdeep.concurrent;

for (int i = n; i < LIMIT; i += 3) {

t0___0

import java.util.concurrent.ExecutorService;

t1___3

if {

回复这几个标题从前供给先领会java八线程的运行机制,线程间通讯机制,线程同步难题。

t1___5

本条难题本身明白的达成方案有二种,一种是依附synchronized和wait/notify,别的一种是基于Lock和Condition.

import java.util.concurrent.locks.ReentrantLock;

e.printStackTrace();

lock.notify();

o.m0();

final ThreadTurnTest1 o = new ThreadTurnTest1();

} catch (InterruptedException e) {

private Object lock = new Object();

再多问多少个难点,为啥要加锁呢?因为唯有加锁能力有限支撑四线程交替有序实行,不然线程的推行是由操作系统随机调治的,那么推行各种自然是乱序的。为啥要对同多少个对象加锁呢?因为唯有对一样对象加锁,技能确定保障访谈该锁的四个线程之间交互通讯(不一致的靶子调用notify不会唤醒处于wait状态的线程),不然正是线程已经获取到锁了,但恐怕会因为没轮到自个儿实行而直白处在wait状态。

1.基于synchronized和wait/notify

synchronized {

private static final int LIMIT = 10000;

private Condition c2 = lock.newCondition();

c1.signal();

private void m1() throws InterruptedException {

if (status != 1) {

t0___2

lock.notify();

status = 0;

RUN0 = false;

/**

}

pool-1-thread-1___12

判断共享状态变量,假使事态值表示还没轮到当前线程实施,则调用调用锁对象的wait方法

}

pool-1-thread-1___3

RUN0 = true;

2.基于Lock和Condition

}

} finally {

if (status != 0) {

}

private Condition c0 = lock.newCondition();

伺机情状变化的线程被提示,试行职务,然后改成状态,然后调用锁对象的notify方法

/**

}

}

private boolean RUN0 = true;

图片 1

@Override

@Override

break;

exec.shutdown();

@Override

exec.submit(o.createTask;

据书上说Lock和Condition能够兑现多个以上线程的通讯。

private Condition c1 = lock.newCondition();

synchronized {

Thread.sleep(10 * 1000);

try {

exec.awaitTermination(5, TimeUnit.MINUTES);

final ThreadTurnTest2 o = new ThreadTurnTest2();

try {

if {

}

}

}

try {

pool-1-thread-3___8

}

public void run() {

* @date 2017年12月21日

pool-1-thread-3___11

System.out.println(Thread.currentThread().getName() + “___” + i);

private static final int LIMIT = 1000;

t0___10

import java.util.concurrent.locks.Lock;

}

巡回内加锁

status = 1;

t0___4

case 0:

*/

public static void main(String[] args) throws InterruptedException {

}

推行结果如下:

pool-1-thread-1___9

pool-1-thread-3___2

}

*/

早已有面试官问过如此几个主题素材,如何让三个线程交替打字与印刷奇数和偶数?怎样让几个线程交替实践?

即使感到写的不利,记得点击关切,借使写的倒霉欢迎争持指正,让我们一并发展!

} catch (InterruptedException e) {

} catch (InterruptedException e) {

for (int i = 1; i < LIMIT; i += 2) {

}

o.m1();

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图