I'm studying Java and I'm working on a very simple code that start 3 threads.
Is composed by 3 classes
TxColor change the color to the text printed in console from the threads
ThNew build and run the threads
ThRandy start the threads (main function)
TxColor
/*
to color the output text
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
class TxColor {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
private static Random r = new Random();
private static String oldpick = "null";
private static ArrayList<String> l = new ArrayList<>(Arrays.asList(ANSI_RESET,ANSI_BLACK,ANSI_RED,ANSI_GREEN,ANSI_BLUE,ANSI_PURPLE,ANSI_CYAN));
static ArrayList<String> getCList(){
return l;
}
// choose a random color thread
static String colPick(){
String col = "null";
int count = 0;
do{
count++;
int n = r.nextInt(l.size());
col = getCList().get(n);
}while (col==oldpick && count<10);
count = 0;
oldpick=col;
return col;
}
}
ThNew
import java.util.Random;
public class ThNew extends Thread {
private String name = "standard";
ThNew(String name){
this.name = name;
}
#Override
public void run(){
try{
sleep(new Random().nextInt(1000));
}catch (Exception e){
e.printStackTrace();
}
System.out.println(TxColor.colPick()+"Thread " + name + " started");
}
}
ThRandy
import java.util.Random;
public class ThRandy {
public static void main(String[] args) {
Random r = new Random();
ThNew th1 = new ThNew("Mario");
ThNew th2 = new ThNew("Giorgio");
ThNew th3 = new ThNew("Andrea");
th1.start();
th2.start();
th3.start();
}
}
In the module colPick() I use the variable count to be sure 100% the code end exiting the loop in case is not able to pick a random color different from the previous one.
If you notice the count variable is not static, so every thread has is own count variable.
I didn't use synchronize to reproduce the issue I will describe you.
When I debug using Intellij https://www.jetbrains.com/idea/ I have this screen
I don't know if is common also in Eclipse or any other IDE, but as you can see in the picture I cannot recognize the int count variable of one thread from another. I only notice to have more than one count because the debug jump forward and back again in the code due to the presence of multiple thread.
There is a way to understand which thread is creating that specific count variable?
Thank you very much for help.
I found the solution and I wish to contribute with my help.
You just open the curtain menu as shown in the picture and you can see every thread with their own variables
An easier approach to this problem is to rearrange the list: pick a random element in the list, swap it with the last element, and always return the last element in the list:
static int e = l.size(); // added
static synchronized String colPick(){
int n = r.nextInt(e);
int last = l.size() - 1;
Collections.swap(l, n, last);
e = last;
return l.get(last);
}
The way this works is a bit subtle.
Initially, e, the range of random numbers you can pick, is the size of the list, so this allows any colour to be picked first time.
The picked element is then swapped with the element at the end of the list
e is then updated to be one less than the size of the list: this means that, on the next iteration, the last element of the list can't be picked. As such, it is guaranteed that the same element can't be picked twice in a row (unless there are two equal elements).
Note that this has to be synchronized for two reasons:
You aren't using a thread-safe random class, such as ThreadSafeRandom.
The swapping and reading of the last item, and the update to e, has to be done atomically.
Related
I am trying to find the longest possible path based on how many connections a variable number has, without repeating connections. The way I thought of doing this was creating a list that holds all points that have already been gone through, but when a path ends, and I need to check a new path, all of those old connections remain in the list. How can I restart my list from the initial point?
Putting it in the recursive function itself would just clear the list each time. Is there a better option than using a list?
Relevant code:
package testapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.IOException;
import java.util.List;
class TestApp {
// Store list of objects we have already matched with
static List<NumberObject> holdingList = new ArrayList<NumberObject>();
//Test objects
static int[] array1 = {2,2};
static int[] array2 = {3,1};
static int[] array3 = {2,1};
static int[] array4 = {1,1};
static NumberObject eight = new NumberObject(array1, 8);
static NumberObject two = new NumberObject(array2, 2);
static NumberObject three = new NumberObject(array3, 3);
static NumberObject four = new NumberObject(array4, 4);
// Test objects ^^
public static int longestSequence(int[][] grid) {
// TODO: implement this function
// Code exists here not relevant to the problem
//Setting up a new numberList array for testing
NumberObject[] newNumberList = {eight, two, three, four};
NumberObject[] connections1 = {two, four};
NumberObject[] connections2 = {two, three};
//Adding connections
eight.connections = connections1;
four.connections = connections2;
for (NumberObject s: newNumberList){
recursive(s);
}
return 0;
}
public static void recursive(NumberObject object){
for (NumberObject x: holdingList){
System.out.println(x);
}
if (!holdingList.contains(object)){
holdingList.add(object);
if (object.hasConnections()){
NumberObject[] newobject = object.getConnections();
for(NumberObject y: newobject){
recursive(y);
}
}
else {
System.out.println(holdingList.size());
return;
}
}
else {
System.out.println(holdingList.size());
return;
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int numRows = 0;
int numCols = 0;
String[] firstLine = reader.readLine().split("\\s+");
numRows = Integer.parseInt(firstLine[0]);
numCols = Integer.parseInt(firstLine[1]);
int[][] grid = new int[numRows][numCols];
for (int row = 0; row < numRows; row++) {
String[] inputRow = reader.readLine().split("\\s+");
for (int col = 0; col < numCols; col++) {
grid[row][col] = Integer.parseInt(inputRow[col]);
}
}
int length = longestSequence(grid);
System.out.println(length);
}
}
class NumberObject {
int[] id;
int value;
NumberObject[] connections;
//Constructor
public NumberObject(int[] id, int value){
this.id = id;
this.value = value;
}
//print statement
public String toString(){
return ("NumberOject: Id = " + id + "\nValue = " + value);
}
//Check if it has connections
public boolean hasConnections(){
if (connections == null){
return false;
}
else if (connections.length != 0){
return true;
}
else
return false;
}
//Return the connections it has
public NumberObject[] getConnections(){
return connections;
}
}
Ideally, the image displays what I want to happen.
Instead, all the old branching connections remain on holdingList.
it should be noted paths can branch off to more than two other objects.
Instead of storing the list in a field, you could just pass an instance of a copy of your list to the function as an argument. So the signature of your function recursive would look like:
public static void recursive(NumberObject object, List<NumberObject> visited)
To hide this implementation detail, I recommend writing two functions, whereby the second function just passes an empty list to the other one.
However, I'd choose a different approach since yours acquires as many new lists as entries are in your tree. In the following implementation, you only have one list per "tree end". Moreover, just like in the previous suggestion, this keeps your class stateless.
static List<NumberObject> findLongestPath(NumberObject currentNode) {
if (currentNode.getConnectedNodes().isEmpty()) {
List<NumberObject> result = new ArrayList<>();
result.add(currentNode);
return result;
}
List<NumberObject> longestPath = currentNode.getConnectedNodes().stream()
.map(PathFinder::findLongestPath)
.max(Comparator.comparing(List::size))
.get();
longestPath.add(currentNode);
return longestPath;
}
I have a fairly straightforward task: I have a list of strings each of which is processed and a score is produced. The string and its score then get added to a map:
public class My1Thread
{
final private static List<String> ids = Arrays.asList("id1","id2","id3","id4","id5");
private static HashMap<String,Double> result = null;
private Double computeResult(String id)
{
Double res = 0.0;
// do stuff to compute result
return res;
}
public static void main(String[] args)
{
result = new HashMap<String,Double>();
for (String id: ids)
{
result.put(id,computeResult(id));
}
}
}
Since scores of any two strings are independent of each other, this seems to be a perfect case to use multithreading. However, I am getting unexpected results, which is probably a typical result for a multithreading newbie.
Here's a m/t version of the above:
public class MyMultiThread
{
final private static int nWorkers = 3; // number of threads
final private static List<String> ids = Arrays.asList("id1","id2","id3","id4","id5");
private static int curIndex = 0; // indexing pointing to position in ids currently being processed
private static HashMap<String,Double> result = null;
public static class Worker implements Runnable {
private int id;
public Worker(int id) {
this.id = id;
}
synchronized void setCounter(final int counter)
{
curIndex = counter;
}
synchronized int getCounter()
{
return curIndex;
}
synchronized void addToResult(final String id, final Double score)
{
result.put(id,score);
}
#Override
public void run()
{
try {
while (true)
{
int index = getCounter();
if (index >= ids.size())
{
// exit thread
return;
}
String id = ids.get(index);
setCounter(index+1);
System.out.print(String.format("Thread %d: processing %s from pos %d\n", id, id, curIndex-1));
Double score = ... // compute score here
addToResult(id,score);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
result = new HashMap<String,ArrayList<Pair<Document,Double>>>();
for (int i = 0; i < nWorkers; i++) {
Thread worker = new Thread(new MyMultiThread.Worker(i));
worker.start();
}
}
}
According to the output produced by System.out.print, this code appears to be processing some elements of ids more than once while not processing others at all. What am I doing wrong here?
Your while(true) loop inside the thread starts at the index specified in the constructor, and then increment it by one, and then the loop starts again. So thread 0 does index 0, then index 1, etc.. Thread 1 does index 1, then index 2, etc... So index 2 will be done 3 times.
I would use a synchronized linked list for ids, and have each thread take and remove the first element of the list, until the list is empty. Use LinkedList.removeFirst().
Also the result hash map also needs to be synchronized, since multiple threads may write to it at the same time.
The problem is that the map is being modified concurrently in multiple threads, so some updates are getting lost.
You declared the methods that modify the map as synchronized, but note that they are synchronized on multiple worker objects: not on a single object, which would provide the locking you are after.
I'd recommend using ConcurrentHashMap and getting rid of all the synchronized declarations.
Some of your synchronization is too narrow - for example, this bit here:
int index = getCounter();
if (index >= ids.size())
{
// exit thread
return;
}
String id = ids.get(index);
setCounter(index+1);
What happens if thread A reads the counter, thread B reads the counter, then thread A updates the counter?
A: int index = getCounter(); // returns 3
B: int index = getCounter(); // returns 3
...
A: setCounter(index + 1); // sets it to 4
B: setCounter(index + 1); // Uh-oh, sets it to 4 as well, we lost an update!
In this case, when you read a variable, then write to it based on the value you read, both the read and the write need to be within the same synchronization block. Declaring getCounter and setCounter as synchronized is not enough.
Simply use Java 8 Stream API :
Map<String, Double> map = ids.parallelStream().collect(Collectors.toConcurrentMap(id -> id, id -> computeScore(id)));
...
Double computeScore(String id) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ThreadLocalRandom.current().nextDouble(100);
}
Here's a nice tutorial.
I made a test program because I am trying to get back into Java after working in PL/SQL. I created a class Numbers that contains an integer, a getter and a setter. I have another class Test that is creating an instance of Numbers, and also adds that instance to a List. I created a for loop that loops two times and sets the value of the integer in Numbers equal to i. I then add that instance of Numbers to the List numbersList. I then do a print screen of the value that was added to the List. I do a total of 3 prints, one print the first time through the loop that prints the first position in the List, then I print two times during the second time through the loop,the first position in the List again, and the second position in the List. I was expecting to get 0,0,1 as the result. I am getting instead 0,1,1 as the result and I cannot figure out why. I am not changing anything in the first position in the List (numbersList[0]) during the second time through the loop, all I am doing is adding an instance of Numbers into the second position in the list (numbersList[1]).
import java.util.ArrayList;
import java.util.List;
public class Tests {
static int x;
public static void main(String[] args) {
List<Numbers> numbersList = new ArrayList<Numbers>();
Numbers numbers = new Numbers();
Numbers numbers2 = new Numbers();
for (int i = 0; i < 2; i++) {
if (i == 0) {
numbers.setVariable(i);
numbersList.add(numbers);
System.out.println(numbersList.get(0).getVariable());
}
if (i > 0) {
numbers2.setVariable(i);
numbersList.add(numbers2);
System.out.println(numbersList.get(0).getVariable());
System.out.println(numbersList.get(1).getVariable());
}
}
}
}
public class Numbers {
public static int a = 5;
public static void setVariable(int b) {
a = b;
}
public static int getVariable() {
return a;
}
}
public static int a = 5 means that all instances of Numbers share the same variable because of the static keyword.
Therefore, when you do numbers2.setVariable(i);, the variable is also changed for numbers. Hence the 0,1,1
If you want instance variables remove the static keywords from Numbers.
Your class Numbers has no instance fields (everything is static, or class level).
It should look something like (and overriding toString() is a good idea),
public class Numbers {
public int a = 5;
public void setVariable(int b){
a = b;
}
public int getVariable(){
return a;
}
#Override
public String toString() {
return String.valueOf(a);
}
}
By overriding toString() you can more easily print instances of Numbers. For example,
System.out.println(numbersList);
One of my objects is a randomly generated number. The other object is derived from using System.nanoTime(). I need to log the time the random number was generated and then store them in an array. I created an addTo method in my storage class. I am calling it from main after I call the methods to generate random number and the time, but I keep getting an error (Storage#1948cc8c). I'm not sure if the number and time I get are even being stored.
public class Driver
{
static MyNum number = new MyNum();
static int numStore;
static Time time = new Time();
static long timeStore;
static Storage storage = new Storage(50);
public static void main (String [] args)
{
for (int i = 0; i < 50; i++)
{
System.out.println(number.myRand());
System.out.println(time.tellTime());
storage.addTo(number, time);
System.out.println(storage);
}
}
}
public class Storage
{
Node[] id;
private int count;
private MyNum m;
private Node n;
public Storage(int size)
{
id = new Node [size];
count = 0;
}
public void addTo(MyNum number, Time time)
{
int size = 0;
for(count=0;count<id.length;count++)
id[size] = new Node(number, time);
}
}
If you don't override toString() in your Storage class, System.out.println(storage); won't display anything helpful to you. You're not getting an error (or rather an exception) here.
i have run into a little problem in here. I am doing a concurrent program in Java. Problem is: There are 4 people (students) that are trying to access printer, to print 5 documents. But only one can print at the time (kind of obvious) 5 documents. When they finish they notify other that they done and other thread accesses the resource. i have a Main class, student class and Monitor (laser printer), Document class that holds info about the document like (number of pages, name user id etc)+ few interfaces for printer. I have managed to run successfully threads but they are not synchronized (mutual exclusion)
So the question is how do i achieve mutual exclusion ( that only one person can print at the time his number of docs)
Thank you for looking, time and hints :)
Main class
String S1Name = "bob";
String S2Name = "klara";
String S3Name = "John";
String S4Name = "Iga";
String T1Name = "Man";
String T2Name = "Woman";
final int NoOfDocs = 5;
ServicePrinter sp = new LaserPrinter();
ThreadGroup groupA = new ThreadGroup("Group A");
ThreadGroup groupB = new ThreadGroup("Group B");
Student student1 = new Student(sp,NoOfDocs,S1Name, groupA);
Student student2 = new Student(sp,NoOfDocs,S2Name, groupA);
Student student3 = new Student(sp,NoOfDocs,S3Name, groupA);
Student student4 = new Student(sp,NoOfDocs,S4Name, groupA);
TonerTechnician TT = new TonerTechnician(groupB);
PaperTechnician PT = new PaperTechnician(groupB);
/*
* Start Student Threads
*/
student1.start();
student2.start();
student3.start();
student4.start();
/*
* Start Technician threads
*/
TT.start();
PT.start();
Student Class
private final ServicePrinter serviceprinter;
private final int NoOfDocs;
private final String Name;
private final ThreadGroup threadgroup;
public Student(ServicePrinter serviceprinter, int NoOfDocs, String Name, ThreadGroup threadgroup)
{
this.serviceprinter = serviceprinter;
this.NoOfDocs = NoOfDocs;
this.Name = Name;
this.threadgroup = threadgroup;
}
#Override
public void run()
{
/*
* each students prints 5 documents (different name and length)
*/
final LaserPrinter lp = new LaserPrinter();
//sleep from 1 to 5 sec random time
final Random random = new Random();
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
StringBuilder sb = new StringBuilder();
/*
* Create random document name 10 characters long
*/
for (int i = 0; i < 10; i++)
{
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String docName = sb.toString();
/*
* print 5 documents (random sleep time between printing)
*/
for(int i = 0; i < NoOfDocs; i++)
{
try
{
Document coursework = new Document(Name,docName,random.nextInt(90)+10);
lp.printDocument(coursework);
Thread.sleep(random.nextInt(1000)+4000);
}
catch (InterruptedException ex)
{
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("User: " + Name+ " completed printing");
Monitor class
int tonerLevel = 500;
int paperLevel = 250;
private final String PrinterName = "HP";
private final String PrinterID = "LX-440";
private int CurrentPaperLevel;
private int CurrentTonerLevel;
private int NoOfDocsPrinted;
#Override
public synchronized void printDocument(Document document) {
System.out.println(document);
}
It seems that you're creating a local printer object in your run method instead of using the shared one you pass to the Student class. Try using the shared printer that you pass and see what you get. Also we need to see how you use printDocument in ServicePrinter. This is because you are using a ServicePrinter object in your Student class, and the implementation of printDocument in ServicePrinter may not be correct (that is if you actually have it implemented in the superclass)
Here is a simple mutex implementation but you should use java.util.concurrent package for synchronization
EDIT: Changed mutex to semaphore (it makes more sense)
A simple mutex implementaion:
public class Mutex {
private int semaphore;
public synchronized void aquire() throws InterruptedException {
if(semaphore < 0) {
wait();
}
semaphore--;
}
public synchronized void release() {
if(semaphore < 0) {
semaphore++;
notify();
}
}
}