对于多线程,经常在笔试面试中被问到如何控制线程有序的输出,题目很简单。要求多线程+有序。
通过代码讲解,实现:
1)定义一个需要创建的线程。
package com.itheima.thread;
import java.util.LinkedHashMap;
import java.util.Map;
public class QueuePrint {
public int targetNum=0; //输出的总数量
public int printOnce=0; //当前次数输出数量
private int nowNum=1; //要打印的数量
/**
*
* @param targetNum 输出的总数量
* @param printOnce 当前次数输出数量
*/
public QueuePrint(int targetNum, int printOnce) {
super();
this.targetNum = targetNum;
this.printOnce = printOnce;
}
private int nextThreadNum=0;//next的下标数
private int threadCount=0;//总的线程数量
//map集合,存放线程,键是具体线程,值存放线程打印的顺序
private Map<Thread,Integer> threads=new LinkedHashMap<Thread,Integer>();
//添加线程
public void setTh(Thread thread) {
threads.put(thread, threadCount);
threadCount++;
}
//运行线程
public void run() {
for (Thread thread : threads.keySet()) {
thread.start();
}
}
public synchronized void printNum() throws InterruptedException {
//获取当前线程
Thread currentThread=Thread.currentThread();
//获取当前线程坐标
int currentNum=threads.get(currentThread);
//判断是否为期望线程
if(currentNum==nextThreadNum) {
for(int i=0;i<printOnce;i++) {
System.out.println("当前线程:"+currentThread.getName()+":"+nowNum++);
if(nowNum>targetNum) {
System.out.println("工作完成");
this.wait();
}
}
//期望线程名+1
nextThreadNum=(++nextThreadNum)%threadCount;
}
}
}2)创建一个执行类package com.itheima.thread;
class RunTest implements Runnable{
QueuePrint ps;
public RunTest(QueuePrint ps ) {
this.ps=ps;
}
@Override
public void run() {
try {
while(true) {
ps.printNum();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3)启动类package com.itheima.thread;
public class ThreadDemo {
public static void main(String [] args) {
//总数量10个,每个线程一次输出5个
QueuePrint ps=new QueuePrint(10, 5);
//增加加线程
ps.setTh(new Thread(new RunTest(ps),"itcast"));
ps.setTh(new Thread(new RunTest(ps),"itheima"));
ps.setTh(new Thread(new RunTest(ps),"chuanzhi"));
ps.run();
}
}
输出结果:"C:\Program Files\Java\jdk1.8.0_171\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50108,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;C:\Users\Administrator\Desktop\Day40\target\classes;E:\maven\repository-2010\mvn_repo\repository1\mysql\mysql-connector-java\5.1.22\mysql-connector-java-5.1.22.jar;E:\maven\repository-2010\mvn_repo\repository1\com\alibaba\druid\1.0.9\druid-1.0.9.jar;C:\Program Files\Java\jdk1.8.0_171\lib\jconsole.jar;C:\Program Files\Java\jdk1.8.0_171\lib\tools.jar;E:\maven\repository-2010\mvn_repo\repository1\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;E:\maven\repository-2010\mvn_repo\repository1\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;E:\maven\repository-2010\mvn_repo\repository1\org\springframework\spring-jdbc\5.0.0.RELEASE\spring-jdbc-5.0.0.RELEASE.jar;E:\maven\repository-2010\mvn_repo\repository1\org\springframework\spring-tx\5.0.0.RELEASE\spring-tx-5.0.0.RELEASE.jar;E:\maven\repository-2010\mvn_repo\repository1\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;E:\maven\repository-2010\mvn_repo\repository1\commons-beanutils\commons-beanutils\1.9.2\commons-beanutils-1.9.2.jar;E:\maven\repository-2010\mvn_repo\repository1\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\maven\repository-2010\mvn_repo\repository1\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\maven\repository-2010\mvn_repo\repository1\com\fasterxml\jackson\core\jackson-databind\2.3.3\jackson-databind-2.3.3.jar;E:\maven\repository-2010\mvn_repo\repository1\com\fasterxml\jackson\core\jackson-core\2.3.3\jackson-core-2.3.3.jar;E:\maven\repository-2010\mvn_repo\repository1\com\fasterxml\jackson\core\jackson-annotations\2.3.3\jackson-annotations-2.3.3.jar;E:\maven\repository-2010\mvn_repo\repository1\redis\clients\jedis\2.7.2\jedis-2.7.2.jar;E:\maven\repository-2010\mvn_repo\repository1\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar;C:\installSoft\developTools\intellij idea\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar" com.Ryan.thread.ThreadDemoConnected to the target VM, address: '127.0.0.1:50108', transport: 'socket'当前线程:itcast:1当前线程:itcast:2当前线程:itcast:3当前线程:itcast:4当前线程:itcast:5当前线程:itheima:6当前线程:itheima:7当前线程:itheima:8当前线程:itheima:9当前线程:itheima:10工作完成
|
|