Java - thread priorities (example from Thinking in Java) - java

I read now chapter about concurrency from Thinking in Java and I try to reproduce example from this book in my computer and I got very different results.
I do example from subchapter Priorities:
public class SimplePriorities implements Runnable {
private int countDown = 5;
private volatile double d;
private int priority;
public SimplePriorities(int priority) {
this.priority = priority;
}
#Override
public String toString() {
return Thread.currentThread() + ": " + countDown;
}
#Override
public void run() {
Thread.currentThread().setPriority(priority);
while (true) {
for (int i = 1; i < 100000; i++) {
d += (Math.PI + Math.E) / (double) i;
if (i % 1000 == 0) {
Thread.yield();
}
System.out.println(this);
if (--countDown == 0) return;
}
}
}
public static void main(String[] args) {
final ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
executorService.execute(new SimplePriorities(Thread.MIN_PRIORITY));
}
executorService.execute(new SimplePriorities(Thread.MAX_PRIORITY));
executorService.shutdown();
}
}
and in result I got:
Thread[pool-1-thread-1,1,main]: 5
Thread[pool-1-thread-4,1,main]: 5
Thread[pool-1-thread-4,1,main]: 4
Thread[pool-1-thread-4,1,main]: 3
Thread[pool-1-thread-4,1,main]: 2
Thread[pool-1-thread-4,1,main]: 1
Thread[pool-1-thread-6,10,main]: 5
Thread[pool-1-thread-6,10,main]: 4
Thread[pool-1-thread-6,10,main]: 3
Thread[pool-1-thread-6,10,main]: 2
Thread[pool-1-thread-6,10,main]: 1
Thread[pool-1-thread-3,1,main]: 5
Thread[pool-1-thread-5,1,main]: 5
Thread[pool-1-thread-2,1,main]: 5
Thread[pool-1-thread-1,1,main]: 4
Thread[pool-1-thread-2,1,main]: 4
Thread[pool-1-thread-5,1,main]: 4
Thread[pool-1-thread-3,1,main]: 4
Thread[pool-1-thread-5,1,main]: 3
Thread[pool-1-thread-2,1,main]: 3
Thread[pool-1-thread-1,1,main]: 3
Thread[pool-1-thread-2,1,main]: 2
Thread[pool-1-thread-5,1,main]: 2
Thread[pool-1-thread-3,1,main]: 3
Thread[pool-1-thread-5,1,main]: 1
Thread[pool-1-thread-2,1,main]: 1
Thread[pool-1-thread-1,1,main]: 2
Thread[pool-1-thread-3,1,main]: 2
Thread[pool-1-thread-1,1,main]: 1
Thread[pool-1-thread-3,1,main]: 1
The author wrote that the last result has the highest priority, but when I run this code in my system I get different priorities and on average often low priorities than high, like above.
I use Ubuntu 16.04.

Related

Adding large matrices ArrayIndexOutOfBoundsException

I am writing a program which adds two integer matrices together and I am running into an ArayIndexOutOfBounds exception when trying to process larger matrices. Any advice on how to fix this issue would be greatly appreciated.
ArrayOutOfBoundsException on cmd prompt
Here is the class in which the error is occuring:
import java.util.ArrayList;
public class ThreadMaker
{
public static int count=0;
public static void addmatrix(int[][] mat_A,int[][] mat_B,int[][] submatrix1,int[][] submatrix2,int N,int M)throws Exception
{
ThreadOperation m1;
ThreadOperation.initialize(N,M);
ArrayList<Thread> threads = new ArrayList<Thread>();
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for(k=0,i=c; i < c + N/2; i++, k++)
{
for(l=0,j=d; j < d + M/2; j++, l++)
{
submatrix1[k][l]=mat_A[i][j];
submatrix2[k][l]=mat_B[i][j];
}
}
m1=new ThreadOperation(mat_A,mat_B,submatrix1,submatrix2,N,M,c,d);
Thread thread =new Thread(m1);
thread.start();
threads.add(thread);
thread.join();
d=d+M/2;
}
c=c+N/2;
}
}
public static void devidematrix(int[][] mat_A,int[][] submatrix,int N,int M)
{
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for( k=0,i=c;i<c+N/2;i++,k++)
{
for( l=0,j=d;j<d+M/2;j++,l++)
{
submatrix[k][l]=mat_A[i][j];
}
}
d=d+M/2;
}
c=c+N/2;
}
}
public synchronized static void printmatrix(int[][] matrix,int row,int col)
{
System.out.print(" ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(matrix[i][j]+" ");
}
}
System.out.print(" ");
}
}
Here is the martix from the matrix2.txt file:
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
If any more information is need please let me know.

Change only one of the many iteration

I have 0-10 for loop I want to change the value to 100 when is only 4 and the rest will not be affected and no break how should I achieve that. And as for the petitioner, do not repel [him].
class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
int n = 0;
for (int i = 0; i < 10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
}
else
{
System.out.println(i);
}
}
}
}
Another Solution!!
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
for (int i = 0; i <=10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
System.out.println(i);
i=4;
continue;
}
else
{
System.out.println(i);
}
}
}
}
Try This one !!
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
for(int i = 0; i < 10; i++){
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if(i == 4){
System.out.println ("100");
}else{
System.out.println(i);
}
}
}

I can not get the multiplication tables between two values recursively 5-8=tables of 6 nd 7

i just know how to get the multiplication table of one number.
i have tryied put another number in method argument but nothing.
input
public static void main(String[] args) {
table(2,10);
}
public static void table(int num,int a) {
if(a>=0) {
table(num,a-1);
System.out.println(num+" x "+a+" = "+num*a);
}
}
output
2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
I do not know why you must use recursive,but maybe you expected is next.
public static void table(int num,int a) {
if(a >= 0 && num <= a) {
for(int i = 0;i <= a;i++){
System.out.println(num+" x "+i+" = "+num*a);
}
if(num<a){
table(num+1,a);
}
}
}

Insertion Sort pt2 -Hackerrank

can someone please check my code as to why the last index is not working as intended? Any advice on how to improve this is much appreciated.
import java.io.*;
import java.util.*;
public class Solution {
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 0 ; i < ar.length-1; i++){
key = ar[i];
seen = i;
while (seen <ar.length-1 && ar[seen+1]<key){
ar[seen]= ar[seen+1];
seen = seen+1;
}
ar[seen]=key;
printArray(ar);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
insertionSortPart2(ar);
}
private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
}
Input (stdin) 6 1 4 3 5 6 2
Your Output (stdout) 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 2 6
Expected Output 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6 Compiler Message Wrong Answer
You are doing it in wrong way. Read the part 1 again.
Assume that first element is sorted. Then try to insert element one by one in sorted list.
Try this:
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 1 ; i < ar.length; i++){
key = ar[i];
seen = i;
while (seen > 0 && ar[seen-1] > key) {
ar[seen] = ar[seen-1];
seen = seen - 1;
}
ar[seen]=key;
printArray(ar);
}
}

Distribute Range of Numbers between each threads

Config File
ThreadSize = 10
StartRange = 1
EndRange = 1000
I have a config file above in which I have number of threads I want to use and the client instance is able to use ID range from 1 to 1000 and suppose the client threads is set at 10, so each thread would have range of 100 id's(basically by dividing end range with thread size) that it can use without stepping on other threads. so What I want is that each thread should use 100 id's from that range without stepping on other threads- for example
Thread1 will use 1 to 100 (id's)
// generate a random number between 1 to 100 and keep on printing values until it has generated all the random values between 1 to 100
Thread2 will use 101 to 200 (id's)
// generate a random number between 101 to 200 and keep on printing values until it has generated all the random values between 101 to 200
Thread3 will use 201 to 300 (id's)
// generate a random number between 201 to 300 and keep on printing values until it has generated all the random values between 201 to 300
-----
----
Thread10 will use 901 to 1000
// generate a random number between 901 to 1000 and keep on printing values until it has generated all the random values between 901 to 1000
I know how to write a multithreading program, but not sure how should I divide the range between various threads.
public static void main(String[] args) {
for (int i = 1; i <= threadSize; i++) {
new Thread(new ThreadTask(i)).start();
}
}
class ThreadTask implements Runnable {
private int id;
public ThreadTask(int id) {
this.id = id;
}
public synchronized void run() {
}
}
Each thread gets N = (EndRange - StartRange + 1) / ThreadSize numbers.
Thread number i gets range (StartRange + i*N) - (StartRange + i*N + N - 1).
In your example N = (1000 - 1 + 1) / 10 = 100.
Thread i = 0 would get range (1 + 0*100) - (1 + 0*100 + 100 - 1) = 1 - 100
Thread i = 1 would get range (1 + 1*100) - (1 + 1*100 + 100 - 1) = 101 - 200
...
public class MyThread extends Thread {
public static final int TOTAL_THREADS = 10;
public static final int START_RANGE = 1;
public static final int END_RANGE = 1000;
public static final int N = (END_RANGE - START_RANGE + 1) / TOTAL_THREADS;
int threadNo;
static volatile int counter = 1;
public final static Object obj = new Object();
public MyThread(int threadNo) {
this.threadNo = threadNo;
}
#Override
public void run() {
synchronized (obj) {
while (counter <= 1000) {
if (counter >= (START_RANGE + threadNo * N) && counter <= (START_RANGE + threadNo * N + N - 1)) {
System.out.println((this.threadNo + 1) + " prints " + counter++);
obj.notifyAll();
} else {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String args[]) {
for (int i = 0; i < TOTAL_THREADS; i++) {
System.out.println("Call thread : " + (i + 1));
MyThread th = new MyThread(i);
th.start();
}
}
}

Categories

Resources