How does java handle Threads?

Im having a problem with my program again xD
Or, well, it’s not necessarily a problem, but I would like to know what’s going on.

My program generate random numbers at a rapid rate, about 200 per second.
I wanted to run several number generations simultaneously so I created some more threads to run almost identical operations. When running the program, one of my processor cores goes up to maybe 70% usage, while the remaining 5 cores stay below 20%. This made me suspect that all the threads might be using the same core, which is not what i want them to do since i want real time simultaneously number generation. So my question is, what is it that causes this? I have looked through the code for quite some time now but im unable to find the source of the problem. Can someone with more experience in this help me out? :slight_smile:

//Kurten

Source:

MainClass


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @version 1.4
 * <p>
 * Last updated 2011-05-21
 */

@SuppressWarnings("serial")
public class MainEngine extends JPanel implements Runnable{

	/*************
	 * Variables!
	 *************/
	//xMins is how many minutes you want the simulation to run for!
	int xMins = 10;
	
	Random rand = new Random();
	
	int letterPosX0 = 7;
	int letterPosY0;
	
	int resultNumber0;
	
	long numbersTotal0 = 0;
	
	//Time variables
	double time1;
	double time2;
	double timeTaken;
	double simulationTime = xMins*(60*1000);
	
	//Booleans for numbers
	boolean is0_0 = false;
	boolean is1_0 = false;
	boolean is2_0 = false;
	boolean is3_0 = false;
	boolean is4_0 = false;
	boolean is5_0 = false;
	boolean is6_0 = false;
	boolean is7_0 = false;
	boolean is8_0 = false;
	boolean is9_0 = false;
	
	//For keeping track of the streaks
	int streak2_0;
	int streak3_0;
	int streak4_0;
	int streak5_0;
	int streak6_0;
	
	int timesCalled0 = 1;
	boolean timesCalledReset0 = false;
	
	boolean redraw0 = false;
	boolean running0 = true;
	boolean drawLetters0 = true;
	
	Graphics2D g2d;
	BufferedImage backbuffer;
	JPanel p1 = new JPanel();
	JFrame f = new JFrame();
	
	//Class objects
	Calculation1 c1;
	Calculation2 c2;
	Calculation3 c3;
	
	//Threads
	Thread calcLoop0;
	Thread calcLoop1;
	Thread calcLoop2;
	Thread calcLoop3;
	
	FileWriter outFile0;
	PrintWriter out0;
	
	
	/*******************************
	 * Code for closing the program
	 *******************************/
	WindowListener windowListener = new WindowAdapter(){
		
		// anonymous WindowAdapter class
		public void windowClosing (WindowEvent w){
			exitProgram();
		} // end windowClosing
	};// end anonymous class

	public void exitProgram(){
		// Whatever application code you want to do on close, e.g.
		c1.running1 = false;
		c2.running2 = false;
		c3.running3 = false;
		running0 = false;
	
		// Whatever code you want to actually close the JFrame, e.g.
		f.setVisible(false);
		f.dispose();
	}
	
	
	/**************************
	 * Main Method starts here
	 * @param args
	 **************************/
	public static void main(String[] args){
		MainEngine me = new MainEngine();
		me.initCalculate();
	}
	
	public void initCalculate(){
		initgfx();
		
		calcLoop0 = new Thread(this);
		calcLoop1 = new Thread(c1);
		calcLoop2 = new Thread(c2);
		calcLoop3 = new Thread(c3);
		
		calcLoop0.start();	
		calcLoop1.start();
		calcLoop2.start();
		calcLoop3.start();
	}

	@Override
	public void run() {
		while(running0){
			
			time1 = System.nanoTime();
			initWriters();
			updateGraphics();
			checkVariables();
			writeToFiles();
			repaint();
			time2 = System.nanoTime() - time1;
			
			try{
				Thread.sleep(5);
			}catch(InterruptedException e){
				e.printStackTrace();
				System.out.println("Interrupted Exception in MainEngine's calcLoop0");
			}
			
			timeTaken += (time2/1000000) + 5;
			if(timeTaken >= simulationTime){
				exitProgram();
			}
		}
	}
	
	/******************************
	 * Initialize the file writers
	 ******************************/
	public void initWriters(){
		try{
        	outFile0 = new FileWriter("C:\\chaos studies\\Mathematical code\\numbers0.txt");
        	out0 = new PrintWriter(outFile0);
		}catch(IOException iex){
			iex.printStackTrace();
			System.out.println("IO exception in MainEngine");
		}
	}
	
	@Override
	public void update(Graphics g){
		System.out.println("updating");
		paint(g);
	}
	
	/********************************************************
	 * Checks if the number computed is a streak is a streak
	 ********************************************************/
	public void checkVariables(){
		switch(resultNumber0){
		case 0:
			if(is0_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is0_0 = true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is0_0 = true;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 1:
			if(is1_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is1_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is1_0 = true;
			is0_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
			
		case 2:
			if(is2_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is2_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is2_0 = true;
			is0_0 = false;
			is1_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 3:
			if(is3_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is3_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is3_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 4:
			if(is4_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is4_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is4_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 5:
			if(is5_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is5_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is5_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 6:
			if(is6_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is6_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is6_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is7_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
			
		case 7:
			if(is7_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is7_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is7_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is8_0 = false;
			is9_0 = false;
			break;
		
		case 8:
			if(is8_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is8_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is8_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is9_0 = false;
			break;
		
		case 9:
			if(is9_0 == false){
				timesCalledReset0 = true;
			}
			if(timesCalledReset0 == true){
				timesCalled0 = 1;
			}
			timesCalledReset0 = false;
			if(is9_0 == true){
				streak2_0++;
				if(timesCalled0 == 2){
					streak2_0--;
					streak3_0++;
				}
				if(timesCalled0 == 3){
					streak2_0--;
					streak3_0--;
					streak4_0++;
				}
				if(timesCalled0 == 4){
					streak2_0--;
					streak4_0--;
					streak5_0++;
				}
				timesCalled0++;
			}
			is9_0 = true;
			is0_0 = false;
			is1_0 = false;
			is2_0 = false;
			is3_0 = false;
			is4_0 = false;
			is5_0 = false;
			is6_0 = false;
			is7_0 = false;
			is8_0 = false;
			break;
		}
	}
	

	
	public void initgfx(){
		backbuffer = new BufferedImage(1680, 1050, BufferedImage.TYPE_INT_RGB);
		g2d = backbuffer.createGraphics();
		f.setSize(1680, 1050);
		//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(this);
		f.setVisible(true);
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, 1680, 1050);
		c1 = new Calculation1(this, g2d);
		c2 = new Calculation2(this, g2d);
		c3 = new Calculation3(this, g2d);
		f.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
		f.addWindowListener( windowListener );
	}

	public void updateGraphics(){
		
		if(drawLetters0 == true){
			g2d.setColor(Color.WHITE);
			resultNumber0 = rand.nextInt(10);
			g2d.drawString("" + resultNumber0, letterPosX0, letterPosY0);
			letterPosX0 += 7;	
		}
		
		numbersTotal0++;
		System.out.println("Total digits " + numbersTotal0);
		
		if(letterPosX0 >= 322){
			letterPosX0 = 7;
			letterPosY0 += 11;
		}
		
		
		if(letterPosY0 >= 1039){
			g2d.copyArea(0, 0, 322, 1050, 0, -11);
			
			redraw0 = true;
			g2d.setColor(Color.WHITE);
			letterPosY0 = 1033;
		}
		
		if(redraw0 = false){
			g2d.setColor(Color.BLACK);
			g2d.drawRect(0, 0, 1680, 1050);
		}
		
		System.out.println("Number0 = " + resultNumber0);
		System.out.println("Times called0 " + timesCalled0);
		System.out.println("streak2_0 " + streak2_0);
		System.out.println("streak3_0 " + streak3_0);
		System.out.println("Streak4_0 " + streak4_0);
		System.out.println("Streak5_ " + streak5_0);
	}
	
	/**
	 * Writes statistics to text files located in the main C: directory
	 */
	public void writeToFiles(){
		out0.println(resultNumber0);
		out0.println("A total amount of " + numbersTotal0 + " digits computed");
		out0.println("Ran for " + (timeTaken/1000) + " Seconds");
		out0.println("Times called current digit: " + timesCalled0);
		out0.println("2 Digits in a row: " + streak2_0);
		out0.println("3 Digits in a row: " + streak3_0);
		out0.println("4 Digits in a row: " + streak4_0);
		out0.println("5 Digits in a row: " + streak5_0);
		out0.close();
	}
	
	public void paint(Graphics g){
		g.drawImage(backbuffer, 0, 0, null);
	}
	
}


import java.awt.Graphics2D;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * @author Kurten
 *
 */

public class Calculation1 implements Runnable{

	Random rand = new Random();
	
	//For measuring time
	double time1;
	double time2;
	double timeTaken;
	
	int letterPosX1 = 329;
	int letterPosY1;
	int number1;
	int resultNumber1;
	long numbersTotal1 = 0;
	
	//Booleans for numbers
	boolean is0_1 = false;
	boolean is1_1 = false;
	boolean is2_1 = false;
	boolean is3_1 = false;
	boolean is4_1 = false;
	boolean is5_1 = false;
	boolean is6_1 = false;
	boolean is7_1 = false;
	boolean is8_1 = false;
	boolean is9_1 = false;
	
	//For keeping track of the streaks
	int streak2_1;
	int streak3_1;
	int streak4_1;
	int streak5_1;
	int streak6_1;
	
	int timesCalled1 = 1;
	boolean timesCalledReset1 = false;
	
	boolean redraw1 = false;
	boolean running1 = false;
	boolean drawLetters1 = true;
	
	FileWriter outFile1;
	PrintWriter out1;
	
	Graphics2D g2d;
	MainEngine me;
	
	//Constructor
	public Calculation1(MainEngine m, Graphics2D g){
		me = m;
		g2d = g;
	}
	
	@Override
	public void run() {
		
		running1 = true;
		
		while(running1){
			try{
				time1 = System.nanoTime();
				initWriters();
				updateGraphics();
				checkVariables();
				writeToFiles();
				time2 = System.nanoTime() - time1;
				Thread.sleep(5);
				timeTaken += (time2/1000000) + 5;
			}catch(InterruptedException e){}
		}
	}
	
	/**
	 * Initialize the file writers
	 */
	public void initWriters(){
		try{
        	outFile1 = new FileWriter("C:\\chaos studies\\Mathematical code\\numbers1.txt");
        	out1 = new PrintWriter(outFile1);
		}catch(IOException iex){}
	}
	
	
	/**
	 * Checks if the number is a streak
	 */
	public void checkVariables(){
		switch(resultNumber1){
		case 0:
			if(is0_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is0_1 = true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is0_1 = true;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 1:
			if(is1_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is1_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is1_1 = true;
			is0_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
			
		case 2:
			if(is2_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is2_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is2_1 = true;
			is0_1 = false;
			is1_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 3:
			if(is3_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is3_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is3_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 4:
			if(is4_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is4_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is4_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 5:
			if(is5_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is5_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is5_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 6:
			if(is6_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is6_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is6_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is7_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
			
		case 7:
			if(is7_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is7_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is7_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is8_1 = false;
			is9_1 = false;
			break;
		
		case 8:
			if(is8_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is8_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is8_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is9_1 = false;
			break;
		
		case 9:
			if(is9_1 == false){
				timesCalledReset1 = true;
			}
			if(timesCalledReset1 == true){
				timesCalled1 = 1;
			}
			timesCalledReset1 = false;
			if(is9_1 == true){
				streak2_1++;
				if(timesCalled1 == 2){
					streak2_1--;
					streak3_1++;
				}
				if(timesCalled1 == 3){
					streak2_1--;
					streak3_1--;
					streak4_1++;
				}
				if(timesCalled1 == 4){
					streak2_1--;
					streak4_1--;
					streak5_1++;
				}
				timesCalled1++;
			}
			is9_1 = true;
			is0_1 = false;
			is1_1 = false;
			is2_1 = false;
			is3_1 = false;
			is4_1 = false;
			is5_1 = false;
			is6_1 = false;
			is7_1 = false;
			is8_1 = false;
			break;
		}
	}
	
	
	public void updateGraphics(){
		resultNumber1 = rand.nextInt(10);
		
		numbersTotal1++;
		
		g2d.drawString("" + resultNumber1, letterPosX1, letterPosY1);
		
		if(letterPosX1 >= 644){
			letterPosX1 = 329;
			letterPosY1 += 11;
		}
		letterPosX1 += 7;
		if(letterPosY1 >= 1039){
			g2d.copyArea(329, 0, 322, 1050, 0, -11);
			letterPosY1 = 1033;
		}
		
		System.out.println("Number1 = " + resultNumber1);
		System.out.println("Times called1 " + timesCalled1);
		System.out.println("streak2_1 " + streak2_1);
		System.out.println("streak3_1 " + streak3_1);
		System.out.println("Streak4_1 " + streak4_1);
		System.out.println("Streak5_1 " + streak5_1);
	}
	
	/**
	 * Writes statistics to text files located in the main C: directory
	 */
	public void writeToFiles(){
		out1.println(resultNumber1);
		out1.println("A total amount of " + numbersTotal1 + " digits computed");
		out1.println("Ran for " + (timeTaken/1000) + " Seconds");
		out1.println("Times called current digit: " + timesCalled1);
		out1.println("2 Digits in a row: " + streak2_1);
		out1.println("3 Digits in a row: " + streak3_1);
		out1.println("4 Digits in a row: " + streak4_1);
		out1.println("5 Digits in a row: " + streak5_1);
		out1.close();
	}
}

I apologize if the code is a bit clunky.
Also, there are 2 more classes which are identical to Calculation1, they are called Calculation2, and 3.

I’m going to completely ignore your question and ask: What do you really want to do here?

Without actualy having understood the code…

Line 197:

if(is0_0 = true){

Highly suspicious.

@Roquen: It’s a program that generates lots of numbers and stores them in text files, its for probability studies =) I want each thread to run on one core each, or split them apart so they don’t use the same core.

@Riven: Oh, thanks for finding that bug, must have forgotten to put a second = sign there, now that’s embarrassing xD

Also, i apologize if the code is long and hard to understand. The entire checkVariables method is a big cluster f**k of if statements and im terribly sorry for that solution, but i couldn’t think of any other way to implement the functionality i wanted.

No, you put in an additional “= true”. There’s no need to compare a boolean to another boolean in order to convert it into a boolean. The only case I can see where it’s useful is “if (x == y)”, which is slightly more transparent than “if (!(x ^y))”.

To answer the original question: I observe that if I have several Java threads doing expensive calculations then all four of my cores show heavy use. I therefore conclude that Java does use native threads correctly, and that what you’re observing is that your calculations aren’t expensive enough for the OS to bother moving the threads across CPUs. I believe that in general it’s good that the OS only uses as many CPUs as are necessary to do the work because that allows the others to be suspended and saves energy.

Thanks for the reply, a friend of mine had a theory that it could be a power saving setting but yours sound more plausible. The thing is, I have some processing power to spare since I run a hexa-core processor. Is it any way to force it to use different cores?

java.util.Random is a 64-bit linear congruent generator. Everything is known about their properties. So, what’s your real question?. You want to consume more of your CPUs? Change the sleeps to zero.

Yes, my real question is how do i make it consume more of my processor’s resources without modifying the sleep if that is possible?
I know that sounds really stupid, but i really want to force it to use all my cores.

Would you suggest using Math.random instead of util.Random?
Im not really that experienced in dealing with random numbers and threads.

EDIT: Seems like your theory was indeed correct pjt33, i changed the sleep time to 1 ms and all my 6 cores went active. The source of the problem was not java, thanks for showing me that :slight_smile:

Also, sorry for confusing you guys.

You can spawn hundreds of threads, if you really want each thread to sleep and and more samples per second.

Hello,
that’s a good bridge to another question:
Does anyone know, how to talk to a specific CPU core?
Par example:
I have 2 Threads, and I want that the first Thread goes to core 1 and the other to core 2 (and I already know that java does that automaticaly), does anyone know how to do that, how to talk to an specific core?

Greetings
biro

Java doesn’t do that, the OS does that for you.

You can set the core affinity of the entire process through the taskmanager.

To set the core-affinity per thread, you need native code. You can’t do that with Java.

Math.random uses java.util.Random :stuck_out_tongue_winking_eye:

If all the threads call Math.random, aren’t they all making use of the same code loaded in RAM? Math.random is a static method, right? So, is everything running through that single piece of code, in whichever core has loaded it?

Or am I conceptually way off here?

I noticed there is this, but it is SE7 and I have no experience with it (for concurrent calls to random number generator):
http://download.oracle.com/javase/tutorial/essential/concurrency/threadlocalrandom.html

Math holds 1 instance of Random. Here is the code inside Math.random:


public static double random() {
    if(randomNumberGenerator == null) initRNG();
    return randomNumberGenerator.nextDouble();
}

private static synchronized void initRNG() {
    if(randomNumberGenerator == null)
        randomNumberGenerator = new Random();
}

@phifrei: Yes, as raking shows explicitly, it’s a static method (which is reentrant) that calls an (unoverrided, thread-safe) method.

WRT: Thread local randoms. Without looking at the code, I really don’t see any point.

[quote]WRT: Thread local randoms. Without looking at the code, I really don’t see any point.
[/quote]
Well, my curiousity has been piqued about a couple aspects, not knowing much about how the JVM works with multiprocessing/parallel processing CPUs. I could see that “which core” might not make any practical difference at this point, though, that we are well below the point where it might impact actual performance.

  1. Will all calls to Math.random() (or to any static method) all invoke the same code, which presumably resides in one or another core of a processor that allows parallel processing?
  2. Will the calls “bring along” any other code to that same cpu?
  3. Will all calls to all static Math methods go to the same cpu/core?
  4. If the future of performance gain comes from the ability to multiprocess, wouldn’t code that allows the system to distribute it across multiple processes run better than code that must be executed via a single “place” in the system?
  5. When distributing processes, does the implementation tend to load one or another subsystem up to a threshold and THEN start offloading processes to other subsystems, or does it try to evenly divide things up from the get-go?
  6. Is there a simple way to make a local (thread-specific) instance of a random number generator if one wanted to? (never mind if this is a pointless activity or not)

PS: my “question” about Math.random being static was rhetorical.

@philfrei
All code that is loaded into RAM can go into either CPU, whether being the same class or not. The OS handles process affinity, not you, and Java rarely interferes. You don’t need to worry how OS’s handle processes. All you need to worry is making darn good games. :smiley:

Math.random() is syncronised, so spawning loads of threads is just going to have then queue up behind each other and be slower than just keeping it all in one thread.

Personally I’d just create one Random object per thread, 'cos it’s simple and obvious. ThreadLoacalRandom is probably using ThreadLocal storage, which is pretty darn slow IIRC.

Math.random() is not synchronized. Neither is Random.

javadoc/Math.random()