多线程是一个计算机性能提升的基础,小伙伴们知道在循环中是否可以运行多线程吗?本篇文章就带你了解下。
例:import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ThreadManager
{
public List > initList = new ArrayList > ();
public int exit = 0; //0正常退出,-1异常退出
public int countFlag = 3; //线程计数
public static long count = 0; //加载次数计数器
public boolean stop = false; //是否线程停止控制标识
public String globalFlag = ""; //控制标识位
private ArrayList workThreadList = new ArrayList (); 线程链表
public ThreadManager()
{
Map map1 = new HashMap ();
Map map2 = new HashMap ();
Map map3 = new HashMap ();
map1.put("A", 1);
map1.put("B", 2);
map2.put("A", 1000);
map2.put("B", 2000);
map3.put("A", 21);
map3.put("B", 22);
initList.add(map1);
initList.add(map2);
initList.add(map3);
for (int s = 0; s
{
globalFlag += "0";
}
System.out.println("~~~初始化标识位:" + globalFlag);
}
public void start()
{
WorkThread worker;
long startTime = new Date()
.getTime();
long endTime = new Date()
.getTime();
for (int i = 0; i
{
Map map = initList.get(i);
worker = new WorkThread(this, i, map);
workThreadList.add(worker);
worker.start(); //线程启动
}
while (!stop)
{
boolean allFlag = true;
for (int s = 0; s
{
if (globalFlag.charAt(s) != '1')
{
allFlag = false;
break;
}
}
if (allFlag)
{
endTime = new Date()
.getTime();
System.out.println("~~~~~~~~~~~~第 " + count + " 打印结束,总耗时:" + (endTime - startTime) + " 毫秒~~~~~~~~~~~~");
try
{
Thread.sleep(3000);
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
count++;
//this.stop=true;
clearGlobalFlag('0');
startTime = new Date()
.getTime();
}
else
{
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
System.exit(this.exit);
}
/**
* 清除标识位
*/
public synchronized void clearGlobalFlag(char c)
{
char[] cst = new char[globalFlag.length()];
for (int s = 0; s
{
cst[s] = c;
}
globalFlag = new String(cst);
}
/**
* 修改指定位对应的标识符
* @param index
* @param c
* @return
*/
public synchronized boolean changeGlobalFlag(int index, char c)
{
boolean bool = false;
char[] cs = globalFlag.toCharArray();
char[] cst = new char[cs.length];
for (int s = 0; s
{
if (s == index)
{
cst[s] = c;
}
else
{
cst[s] = cs[s];
}
}
globalFlag = new String(cst);
bool = true;
return bool;
}
public String getGlobalFlag()
{
return globalFlag;
}
/**
* @param args
*/
public static void main(String[] args)
{
ThreadManager threadManager = new ThreadManager();
threadManager.start();
}
public int getExit()
{
return exit;
}
public synchronized void setExit(int exit)
{
this.exit = exit;
}
}import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class WorkThread extends Thread
{
private ThreadManager threadManager;
private Map map = new HashMap ();
private int index = 0;
public WorkThread(ThreadManager threadManager, int index, Map map)
{
this.threadManager = threadManager;
this.index = index;
this.map.putAll(map);
}
public void run()
{
while (!threadManager.stop)
{
int count = 0;
long startTime = 0;
long endTime = 0;
String msg = "~~~ 线程" + index + " 打印Map信息";
if (threadManager.getGlobalFlag()
.charAt(index) == '0')
{
startTime = new Date()
.getTime();
System.out.println("--A--" + map.get("A") + "--B--" + map.get("B"));
endTime = new Date()
.getTime();
threadManager.changeGlobalFlag(index, '1');
System.out.println("--耗时:" + (endTime - startTime) + " 毫秒");
}
else
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
结果:~~~初始化标识位: 000
--A--1--B--2
--耗时: 0 毫秒
--A--1000--B--2000
--耗时: 0 毫秒
--A--21--B--22
--耗时: 0 毫秒~~~~~~~~~~~~第 0 打印结束, 总耗时: 154 毫秒~~~~~~~~~~~~
--A--1000--B--2000
--耗时: 0 毫秒
--A--21--B--22
--耗时: 0 毫秒
--A--1--B--2
--耗时: 0 毫秒~~~~~~~~~~~~第 1 打印结束, 总耗时: 150 毫秒~~~~~~~~~~~~
--A--1--B--2
--耗时: 0 毫秒
--A--1000--B--2000
--耗时: 0 毫秒
--A--21--B--22
--耗时: 0 毫秒~~~~~~~~~~~~第 2 打印结束, 总耗时: 150 毫秒~~~~~~~~~~~~
看见了吧,在循环中运行多线程可以帮助提升更高性能,有兴趣的小伙伴们可以尝试下。
以上就是本文的所有内容,有关更多java常见问题及解决方法请关注我们了解详情。
推荐阅读: