SidKid
SidKid
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/18/2025 in #java-help
could you explain what this code is doing in terms of implementing the problem in the picture?
^thats all one code, discord was saying it is too long to be in one block
11 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/18/2025 in #java-help
could you explain what this code is doing in terms of implementing the problem in the picture?
// Example 2: Showing a correct implementation with synchronizedIncrement()

Counter counter2 = new Counter();

Thread[] threads2 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads2[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter2.synchronizedIncrement();

}

});

threads2[i].start();

}



for (Thread thread : threads2) {

thread.join();

}



System.out.println("Synchronized Example: Expected: " + (numThreads * numIncrements) + ", Actual: " + counter2.getCount());

// Example 3: Showing a correct implementation with betterIncrement() (atomic)

Counter counter3 = new Counter();

Thread[] threads3 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads3[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter3.betterIncrement();

}

});

threads3[i].start();

}



for (Thread thread : threads3) {

thread.join();

}



System.out.println("Atomic Example: Expected: " + (numThreads * numIncrements) + ", Actual: " + counter3.getCount());



}

}
// Example 2: Showing a correct implementation with synchronizedIncrement()

Counter counter2 = new Counter();

Thread[] threads2 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads2[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter2.synchronizedIncrement();

}

});

threads2[i].start();

}



for (Thread thread : threads2) {

thread.join();

}



System.out.println("Synchronized Example: Expected: " + (numThreads * numIncrements) + ", Actual: " + counter2.getCount());

// Example 3: Showing a correct implementation with betterIncrement() (atomic)

Counter counter3 = new Counter();

Thread[] threads3 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads3[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter3.betterIncrement();

}

});

threads3[i].start();

}



for (Thread thread : threads3) {

thread.join();

}



System.out.println("Atomic Example: Expected: " + (numThreads * numIncrements) + ", Actual: " + counter3.getCount());



}

}
11 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/18/2025 in #java-help
could you explain what this code is doing in terms of implementing the problem in the picture?
public class RaceConditionExample {



public static void main(String[] args) throws InterruptedException {

int numThreads = 1000;

int numIncrements = 1000;



// Example 1: Demonstrating the race condition with brokenIncrement()

Counter counter1 = new Counter();

Thread[] threads1 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads1[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter1.brokenIncrement();

}

});

threads1[i].start();

}



for (Thread thread : threads1) {

thread.join(); // Wait for all threads to finish

}



System.out.println("Race Condition Example (brokenIncrement): Expected: " + (numThreads * numIncrements) + ", Actual: " + counter1.getCount());
public class RaceConditionExample {



public static void main(String[] args) throws InterruptedException {

int numThreads = 1000;

int numIncrements = 1000;



// Example 1: Demonstrating the race condition with brokenIncrement()

Counter counter1 = new Counter();

Thread[] threads1 = new Thread[numThreads];



for (int i = 0; i < numThreads; i++) {

threads1[i] = new Thread(() -> {

for (int j = 0; j < numIncrements; j++) {

counter1.brokenIncrement();

}

});

threads1[i].start();

}



for (Thread thread : threads1) {

thread.join(); // Wait for all threads to finish

}



System.out.println("Race Condition Example (brokenIncrement): Expected: " + (numThreads * numIncrements) + ", Actual: " + counter1.getCount());
11 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/18/2025 in #java-help
could you explain what this code is doing in terms of implementing the problem in the picture?
class Counter {

private int count = 0;



public int getCount() {

return count;

}



public void increment() {

count++;

}



//The synchronized keyword makes this method thread-safe.

public synchronized void synchronizedIncrement() {

count++;

}





public void brokenIncrement() {

int temp = count; // Read

count = temp + 1; // Write

}



public void betterIncrement() {

count++;

}

}
class Counter {

private int count = 0;



public int getCount() {

return count;

}



public void increment() {

count++;

}



//The synchronized keyword makes this method thread-safe.

public synchronized void synchronizedIncrement() {

count++;

}





public void brokenIncrement() {

int temp = count; // Read

count = temp + 1; // Write

}



public void betterIncrement() {

count++;

}

}
11 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/5/2025 in #java-help
question about Java
@Kyo-chan
9 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 2/5/2025 in #java-help
question about Java
Why is it relevant that there was no Java on Macs?
9 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 1/29/2025 in #java-help
attempted this code but still not working
6 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 1/27/2025 in #java-help
how to solve this without loops at all
public int count11(String str) {
//what happens when all 1s
if(str.length() < 2){ //cannot possibly have 11 which is 2 chracters long if string less than 2
return 0;
}
//going through string using substrings (passing substrings into the recursive function)

if(str.substring(0,1) == "11"){
if(str.length() == 2){
return count11(str.substring(1)) + 1;
}
else if (str.substring(2,2) == "1") {
return count11(str.substring(1));
}
else {
return count11(str.substring(1))+1;
}
}
else{
return count11(str.substring(1));
}
}
public int count11(String str) {
//what happens when all 1s
if(str.length() < 2){ //cannot possibly have 11 which is 2 chracters long if string less than 2
return 0;
}
//going through string using substrings (passing substrings into the recursive function)

if(str.substring(0,1) == "11"){
if(str.length() == 2){
return count11(str.substring(1)) + 1;
}
else if (str.substring(2,2) == "1") {
return count11(str.substring(1));
}
else {
return count11(str.substring(1))+1;
}
}
else{
return count11(str.substring(1));
}
}
15 replies
JCHJava Community | Help. Code. Learn.
Created by SidKid on 1/27/2025 in #java-help
how to solve this without loops at all
do you see what could be the problem with this attempt at the question?
15 replies