patternjavaMinor
N-queens multithreaded working time
Viewed 0 times
workingqueensmultithreadedtime
Problem
I coded a linear (without threads) app, which looks ok and has normal solving speed. Also, I coded a multithreaded app, and it is ok when I use 2 processors and 2 threads. It is also fine when I use 4 processors and 4 threads. But when I try to solve with 6 processors and 6 threads, I am not satisfied with the solving speed. How can I make it faster?
Main.java:
Karalienes.java:
```
import java.util.Stack;
public class Karalienes extends Thread {
private int n;
private int places[];
private int column;
private static Integer solutions = new Integer(0);
private static Integer startedThreads = 0;
private static int maxThreads;
private Stack stack;
public Karalienes(int n,int column, int places[]) {
this.n = n;
this.column = column;
this.places = places;
stack = new Stack();
}
public static void setMaxGSK(int sk) {
this.maxThreads = sk;
}
private boolean arOk(int row, int column) {
boolean ok = true;
int tmpColumn = column-1;
for(; tmpColumn>=1; tmpColumn--) {
if ((places[tmpColumn]-row)==0) {
Main.java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//Scanner scanner = new Scanner(System.in);
//System.out.println("Iveskite N:");
//int n = Integer.parseInt(scanner.nextLine());
int n = Integer.parseInt(args[0]);
int maxThreads = Integer.parseInt(args[1]);
Karalienes.setMaxGSK(maxThreads);
Karalienes queens = new Karalienes(n, 1, new int[n+1]);
long startedTime = System.currentTimeMillis();
queens.start();
queens.join();
System.out.println("Solutions: "+Karalienes.getVariantuSkaiciu());
//queens.print();
System.out.println("Working time: "+(float)(System.currentTimeMillis()-startedTime)/1000
+ " s.");
}
}Karalienes.java:
```
import java.util.Stack;
public class Karalienes extends Thread {
private int n;
private int places[];
private int column;
private static Integer solutions = new Integer(0);
private static Integer startedThreads = 0;
private static int maxThreads;
private Stack stack;
public Karalienes(int n,int column, int places[]) {
this.n = n;
this.column = column;
this.places = places;
stack = new Stack();
}
public static void setMaxGSK(int sk) {
this.maxThreads = sk;
}
private boolean arOk(int row, int column) {
boolean ok = true;
int tmpColumn = column-1;
for(; tmpColumn>=1; tmpColumn--) {
if ((places[tmpColumn]-row)==0) {
Solution
You're probably encountering locking between threads, where the
synchronized between threads is costing you more than you gain. Here's what I'd do:- Instead of using
synchronizedto protect your counters I'd useAtomicIntegerto reduce the amount of locking between threads.
- Instead of creating and lots of short-lived threads yourself, I'd use a a thread pool (e.g.
ThreadPoolExecutor. You can usesetMaximumPoolSizeto restrict the number of threads being used. The executor service will provide a queue of jobs.
Context
StackExchange Code Review Q#10643, answer score: 4
Revisions (0)
No revisions yet.