高分多線程問題
大家好,
有关knight's tour的問題 現在我想用多線程 幷發該如何解決 主要是用到生產者消費者以及工作隊列。要求worker不斷從工作隊列中獲得Path p 然後進行處理 如果不清楚knight's tour的 可以看下我下面的代碼,只要把這段代碼改成幷發執行的 就OK了,時間在26號中午之前。先謝謝了!!
有关knight's tour的問題 現在我想用多線程 幷發該如何解決 主要是用到生產者消費者以及工作隊列。要求worker不斷從工作隊列中獲得Path p 然後進行處理 如果不清楚knight's tour的 可以看下我下面的代碼,只要把這段代碼改成幷發執行的 就OK了,時間在26號中午之前。先謝謝了!!
// Find a *single* closed knight's tour
// (Sequential & iterative version, not optimised)
import java.util.*;
class Position {
static final int numRows = 3; // number of rows on board
static final int numCols = 10; // number of columns on board
static final Position end1 = new Position(1,2); // possible end positions (start is (0,0)
static final Position end2 = new Position(2,1); // other possible end position
private int row; // row number, o<=i<numRows
private int col; // column number, o<=j<numCols
Position(int row0,int col0) {
row = row0; col = col0;
}
public boolean equals(Object obj) { // compare with another position
if (obj==null) return false;
Position p = (Position) obj;
return (p.row==row && p.col==col);
}
Position[] knightMoves() { //list of knight's possible moves from here
Position[] temp = new Position[8];
int count = 0; // temp[0..count-1] holds results
int[] xStep = {2, 2, -2, -2, 1, 1, -1, -1}; // potential moves are (row+yStep[k],col+xStep[k]) for each index k
int[] yStep = {1, -1, 1, -1, 2, -2, 2, -2};
for (int k=0; k<xStep.length; k++) { // check that each potential move stays on board
int x = row+yStep[k];
int y = col+xStep[k];
if (0<=x && x<numCols && 0<=y && y<numRows) {
temp[count] = new Position(x,y); count++;
}
}
Position[] result = new Position[count];
for (int k=0; k<count; k++)
result[k] = temp[k];
return result;
}
void putPosition() { // print position
System.out.print("("+row+","+col+")");
}
}
class Path { // non-empty path of Position's
private Position start; // one end position in path (other is (0,0))
private Path next; // path following start node (may be null)
private int pathLength; // length of path
Path(Position start0, Path next0) {
start = start0; next = next0;
if (next==null)
pathLength = 1;
else
pathLength = 1 + next.pathLength;
}
boolean contains(Position p) { // does p occur in this path?
if (start.equals(p)) return true;
else if (next==null) return false;
else return next.contains(p);
}
Position[] pathMoves() { // new positions to extend path
return start.knightMoves();
}
boolean pathComplete() { // does the path cover the entire board?
int tourLength = Position.numRows*Position.numCols;
return pathLength == tourLength;
}
boolean pathClosed() { // is the path (assumed complete) closed?
return start.equals(Position.end1) || start.equals(Position.end2);
}
void putPath() { // print path
start.putPosition();
if (next!=null) {
System.out.print(":");
next.putPath();
}
}
}
class KnightsTourSingleItSeq {
private static void knightsTour() {
LinkedList<Path> workQ = new LinkedList<Path>();
workQ.add(new Path(new Position(0,0), null));
while (!workQ.isEmpty()) {
Path path = workQ.removeLast();
Position[] moves = path.pathMoves();
for (Position pos : moves) {
if (!path.contains(pos)) {
Path newPath = new Path(pos, path);
if (newPath.pathComplete()) {
if (newPath.pathClosed())
newPath.putPath();
System.out.println();
return;
}
else workQ.add(newPath);
}
}
}
}
public static void main(String[] args) {
long startTime = System.nanoTime();
knightsTour();
long endTime = System.nanoTime();
long time = (endTime-startTime)/1000000;
System.out.println("Running time = " + time);
}
}
...全文
相关推荐
RV1126 多线程获取高分辨率和低分辨率
在编程实现多线程时,开发者需要考虑到线程之间的同步和通信问题,以防止数据访问冲突和提高资源利用率。例如,使用互斥锁(mutex)来保护共享资源,使用信号量(semaphore)来进行线程间的同步等。
解决高分辨率摄像头在使用OpenCV时出现的卡顿和不流畅问题
在使用OpenCV进行高分辨率摄像头图像处理时,有时会遇到卡顿和不流畅的问题。本文将介绍一些解决这个问题的方法,包括使用多线程处理图像、优化图像采集和处理流程、调整摄像头参数等。通过使用多线程处理图像、优化图像采集和处理流程、调整摄像头参数等方法,可以有效解决高分辨率摄像头在使用OpenCV时出现的卡顿和不流畅的问题。这些方法可以提高图像处理的速度和流畅度,从而更好地应对高分辨率摄像头的需求。这样,图像采集和处理过程就可以并行进行,提高了处理速度和流畅度。获取摄像头的图像帧,然后将帧存储在。
参加百度高分杯等竞赛的代码.zip
**并行计算与多线程**:某些竞赛可能涉及大数据处理,这时并行计算和多线程技术的应用就显得尤为重要。
高分辨处理.zip
实时渲染技术需要高性能计算,包括GPU加速、多线程优化等,来应对高分辨率带来的计算压力。4. **存储与传输**:高分辨率文件的存储和传输是挑战。
多线程 启动控制台 精确定时 WaitForSingleObject
多线程可以提高程序的执行效率,特别是在CPU密集型或I/O密集型任务中,通过并行处理不同任务,可以充分利用处理器资源。然而,多线程也带来了同步和互斥的问题,需要妥善管理共享资源,防止数据竞争和死锁。


Java SE
