I have a singleton class and a method with an endless loop. I would like to call that a hundred times with 100 threads inside getInstance. The threads are created, but I only have 60-70 object which are created by the infinite loop.
I'm open to any idea.
I've already tried with newCachedThreadPool and newFixedThreadPool.
public final class MyClass {
private static MyClass instance = null;
public static boolean stop;
private static Map<Integer, CustomBean> pieces = new HashMap<>();
public static MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
for (int i = 0; i < 100; i++) {
Executors.newSingleThreadExecutor().execute(() -> {
try {
endlessMagic();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
return instance;
}
public static void endlessMagic() throws InterruptedException {
while (!stop) {
// where magic happens
pieces.put(something);
Thread.sleep(20);
}
}
}
public class MyClassServletContextListener implements ServletContextListener {
#Override public void contextInitialized(ServletContextEvent servletContextEvent) {
MyClass.getInstance();
}
#Override public void contextDestroyed(ServletContextEvent servletContextEvent) {
MyClass.stop = true;
}
}
Related
```
package programs;
public class TestThreads {
public static void main(String[] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
Thread one = new Thread(t1);
Thread two = new Thread(t2);
one.start();
two.start();
}
}
class Accum{
private static Accum a = new Accum();
private int counter = 0;
private Accum() {
}
public static Accum getAccum() {
return a;
}
public void updateCounter(int add) {
counter +=add;
}
public int getCount() {
return counter;
}
}
class ThreadOne implements Runnable{
Accum a = Accum.getAccum();
#Override
public void run() {
for(int x=0;x<98;x++) {
a.updateCounter(1000);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("one "+ a.getCount());
}
}
class ThreadTwo implements Runnable{
Accum a = Accum.getAccum();
#Override
public void run() {
for(int x=0;x<99;x++) {
a.updateCounter(1);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("two "+ a.getCount());
}
}
```
The expected output should be as below
One 98098
Two 98099
But I am getting values same for both One and Two.
Is this expected or both should result in different values?
When it comes to thread priorities eventhough the jvm scheduler takes the role to choose the turn of which thread should execute first, what about the results of this program where two void run programs of for loop with 98 and 99 which should result in two different values else the same
This is because shared data is NOT synchronized.
To resolve this problem, use synchronized on methods those touch the shared data, to make Accum class Thread-Safe.
example:
class Accum{
private static Accum a = new Accum();
private int counter = 0;
private Accum() {
}
public static Accum getAccum() {
return a;
}
public synchronized void updateCounter(int add) {
counter +=add;
}
public synchronized int getCount() {
return counter;
}
}
Maybe make the methods and fields in the Accum class static, because then it is saved in a general place and not an instance...
Code:
class Accum{
private static Accum a = new Accum();
private static int counter = 0;
private Accum() {
}
public static Accum getAccum() {
return a;
}
public static void updateCounter(int add) {
counter +=add;
}
public static int getCount() {
return counter;
}
}
class ThreadTwo implements Runnable{
#Override
public void run() {
for(int x=0;x<99;x++) {
Accum.updateCounter(1);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("two "+ Accum.getCount());
}
}
class ThreadOne implements Runnable{
#Override
public void run() {
for(int x=0;x<98;x++) {
Accum.updateCounter(1000);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("one "+ Accum.getCount());
}
}
I am not very experienced in Java yet, so this might not work...
[EDIT] I tested it and I believe it works
Hi I have implemented my code like below two snippets but still I am not able to achieve synchronization in threads, as my methods are synchronized still no thread is waiting for other till complete. can you please let me know what mistake did I make in below snippet?
but when I put sleep on Thread in between then it works fine but that is not better way to implement by putting sleep on Thread
Option 1:
public class A{
public static void main(String args[]){
C c = new C();
ExecutorService executorService = Executors.newFixedThreadPool(10);
B t1 = new B(c);
for(int i = 1; i <=10; i++) {
executorService.submit(/*new B(c)*/t1);
}
executorService.shutdown();
try{
executorService.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
}catch(InterruptedException e){
System.out.println("Error while checking tread life: "+e);
}
}
}
public class B extends Thread{
C c;
static final Object lockObject = new Object();
public B(C c) {
this.c = c;
}
public void run(){
someProcess();
}
synchronized public void someProcess(){
String something = c.C();
if(something == null || "".equalsIgnoreCase(something)){
c.A();
}else{
c.B();
}
}
}
public class C {
synchronized public void method A(){
//insert the recods
}
synchronized public void method B(){
//update the records
}
synchronized public String method C(){
// searching a record to get it's id if it exist
return something;
}
}
Option 2:
public class A {
public static void main(String args[]) {
C c = new C();
ExecutorService executorService = Executors.newFixedThreadPool(10);
B t1 = new B(c);
for(int i = 1; i <=10; i++) {
executorService.submit(/*new B(c)*/t1);
}
if(executorService !=null) {
executorService.shutdown();
try {
executorService.awaitTermination (Long.MAX_VALUE,TimeUnit.NANOSECONDS);
}catch(InterruptedException e) {
System.out.println("Error while checking tread life: "+e);
}
}
}
}
public class B extends Thread {
C c;
static final Object lockObject = new Object();
public B(C c) {
this.c = c;
}
public void run() {
someProcess();
}
public void someProcess() {
synchronized(this) {
String something = c.C();
if(something == null || "".equalsIgnoreCase(something)) {
c.A();
}else{
c.B();
}
}
}
}
public class C {
synchronized public void method A() {
//insert the recods
}
synchronized public void method B() {
//update the records
}
synchronized public String method C() {
// searching a record to get it's id if it exist
return something;
}
}
Option 3:
public class A {
public static void main(String args[]) {
C c = new C();
ExecutorService executorService = Executors.newFixedThreadPool(10);
B t1 = new B(c);
for(int i = 1; i <=10; i++) {
executorService.submit(/*new B(c)*/t1);
}
if(executorService !=null) {
executorService.shutdown();
try {
executorService.awaitTermination (Long.MAX_VALUE,TimeUnit.NANOSECONDS);
}catch(InterruptedException e) {
System.out.println("Error while checking tread life: "+e);
}
}
}
}
public class B extends Thread {
C c;
static final Object lockObject = new Object();
public B(C c) {
this.c = c;
}
public void run() {
someProcess();
}
public void someProcess() {
synchronized(this) {
String something = c.C();
if(something == null || "".equalsIgnoreCase(something)) {
c.A();
}else{
c.B();
}
}
}
}
public class C {
public void method A() {
synchronized(this) {
//insert the recods
}
}
public void method B() {
synchronized(this) {
//update the records
}
}
public String method C() {
synchronized(this) {
// searching a record to get it's id if it exist
return something;
}
}
}
Option 4:
public class A {
public static void main(String args[]) {
C c = new C();
ExecutorService executorService = Executors.newFixedThreadPool(10);
B t1 = new B(c);
for(int i = 1; i <=10; i++) {
executorService.submit(/*new B(c)*/t1);
}
if(executorService !=null) {
executorService.shutdown();
try {
executorService.awaitTermination (Long.MAX_VALUE,TimeUnit.NANOSECONDS);
}catch(InterruptedException e) {
System.out.println("Error while checking tread life: "+e);
}
}
}
}
public class B extends Thread {
C c;
static final Object lockObject = new Object();
public B(C c) {
this.c = c;
}
public void run() {
someProcess();
}
public void someProcess() {
synchronized(lockObject) {
String something = c.C(lockObject);
if(something == null || "".equalsIgnoreCase(something)) {
c.A(lockObject);
}else{
c.B(lockObject);
}
}
}
}
public class C {
public void method A(Object lockObject) {
synchronized(lockObject) {
//insert the recods
}
}
public void method B(Object lockObject) {
synchronized(lockObject) {
//update the records
}
}
public String method C(Object lockObject) {
synchronized(lockObject) {
// searching a record to get it's id if it exist
return something;
}
}
}
DefaultRunners are producers
and OrderTaker is a consumer
They both share a OrderQueue.
Currently, I use the variable isDone to indicate if a game is finished.
Once each round is done, I want to make it repeat again and again.
However, in my current implementation it will only run once.
How could I solve it?
public class OrderQueue {
public synchronized void pushOrder(Order order) throws InterruptedException {
if (isDone) {
wait();
} else {
runnersQueue.addLast(order);
notifyAll();
}
}
public void pullOrder() {
try {
if (runnersQueue.size() == 0) {
} else if (isDone) {
wait();
} else {
handleOrder(runnersQueue.pop());
}
} catch (InterruptedException e) {
}
}
In my main class
while(true){
enterYesToStart();
DefaultRunners dfltRunner = new DefaultRunners(queue);
OrderTaker taker = new OrderTaker(queue);
taker.run();
System.out.println("This round is finished"); # never reach to this line
}
Here's the full source code for the example
https://gist.github.com/poc7667/d98e3bf5b3b470fcb51e00d9a0d80931
I've taken a look at your code snippets and the problem is fairly obvious.
The main thread runs the OrderTaker runnable. The main thread is stuck in an eternal loop as the while statement cannot complete unless it throws an exception. (Note that the same is true for your ThreadRunner runnable.)
This means that the main thread i still pulling orders while the race is already done.
The OrderTaker should exit it's while loop while once the race is done. I guess that there are multiple ways achieve this, but one way is use a shared variable.
I took your code and adapted it into a working example.
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class RaceApp {
public static void main(String[] args) throws InterruptedException {
final RaceUpdateManager queue = new RaceUpdateManager();
for (int i = 0; i < 3; i++) {
queue.reset();
List<Thread> threads = Arrays.asList(
new Thread(new Runner("Tortoise", 0, 10, queue)),
new Thread(new Runner("Hare", 90, 100, queue))
);
for (Thread thread : threads) {
thread.start();
}
RaceUpdatesProcessor processor = new RaceUpdatesProcessor(queue);
processor.run();
System.out.println("Game finished");
}
}
private static class RaceUpdateManager {
private static final int TOTAL_DISTANCE = 300;
//thread-safe implementation for queue so no external syncrhonization is required when adding/removing updates
private final Deque<RaceUpdate> runnersQueue = new ConcurrentLinkedDeque<>();
//lock used to sync changes to runnersRecords and done variables
private final ReadWriteLock raceStatusLock = new ReentrantReadWriteLock();
private final Map<String, Integer> runnersRecords = new HashMap<>();
private volatile boolean raceDone = false;//volatile keyword guarantees visibility of changes to variables across threads
public boolean isRaceDone() {
return raceDone;
}
//updates can by added simultaneously (read lock)
public void register(RaceUpdate raceUpdate) throws InterruptedException {
Lock readLock = raceStatusLock.readLock();
readLock.lock();
try {
if (!raceDone) {
runnersQueue.addLast(raceUpdate);
}//ignore updates when the race is done
} finally {
readLock.unlock();
}
}
//but they need to be processed in order (exclusive write lock)
public void processOldestUpdate() {
Lock writeLock = raceStatusLock.writeLock();
writeLock.lock();
try {
RaceUpdate raceUpdate = runnersQueue.poll();
if (raceUpdate != null) {
handleUpdate(raceUpdate);
}
} finally {
writeLock.unlock();
}
}
private void handleUpdate(RaceUpdate raceUpdate) {
Integer distanceRun = runnersRecords.merge(
raceUpdate.runner, raceUpdate.distanceRunSinceLastUpdate, (total, increment) -> total + increment
);
System.out.printf("%s: %d\n", raceUpdate.runner, distanceRun);
if (distanceRun >= TOTAL_DISTANCE) {
raceDone = true;
System.out.printf("Winner %s\n", raceUpdate.runner);
}
}
public void reset() {
Lock writeLock = raceStatusLock.writeLock();
writeLock.lock();
try {
runnersQueue.clear();
runnersRecords.clear();
raceDone = false;
} finally {
writeLock.unlock();
}
}
}
public static class Runner implements Runnable {
private final String name;
private final int rest;
private final int speed;
private final RaceUpdateManager queue;
private final Random rand = new Random();
public Runner(String name, int rest, int speed, RaceUpdateManager queue) {
this.name = name;
this.rest = rest;
this.speed = speed;
this.queue = queue;
}
#Override
public void run() {
while (!queue.isRaceDone()) {
try {
if (!takeRest()) {
queue.register(new RaceUpdate(this.name, this.speed));
}
Thread.sleep(100);
} catch (InterruptedException e) {
//signal that thread was interrupted and exit method
Thread.currentThread().interrupt();
return;
}
}
}
private boolean takeRest() {
return rand.nextInt(100) < rest;
}
}
public static class RaceUpdatesProcessor implements Runnable {
private final RaceUpdateManager queue;
public RaceUpdatesProcessor(RaceUpdateManager queue) {
this.queue = queue;
}
#Override
public void run() {
while (!queue.isRaceDone()) {
try {
queue.processOldestUpdate();
Thread.sleep(50);
} catch (InterruptedException e) {
//signal that thread was interrupted and exit method
Thread.currentThread().interrupt();
return;
}
}
}
}
public static class RaceUpdate {
public final String runner;
public final int distanceRunSinceLastUpdate;
public RaceUpdate(String runner, int distanceRunSinceLastUpdate) {
this.runner = runner;
this.distanceRunSinceLastUpdate = distanceRunSinceLastUpdate;
}
}
}
class hehe implements Runnable {
static int count = 0;
public synchronized void count() {
count++;
}
public void run() {
for (int i = 0; i < 10000; i++) {
count();
}
}
}
public class Sychronise {
public static void main(String[] args) {
Thread a1 = new Thread(new hehe());
Thread a2 = new Thread(new hehe());
a1.start();
a2.start();
try {
a1.join();
a2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(hehe.count);
}
}
The count should be 20000, but my output is still unstable.
Currently, your synchronized does nothing as it's synchronizing on each individual 'hehe' instance. To make it synchronize on the Class object (which owns the 'count' variable) make your count method static too:
public static synchronized void count(){
count++;
}
Or synchronize on an object:
public class hehe implements Runnable {
static int count= 0;
static Object lock = new Object();
public void count(){
synchronized(lock) {
count++;
}
}
public void run(){
for (int i=0;i<10000;i++){
count();
}
}
}
The issue here is that your count method is synchronized but there are 2 instances of the object Hehe, so each synchronization would be scoped to each instance, in this case I would suggest to use an AtomicInteger
First try with a static count method.
public static synchronized void count(){
count++;
}
public void run(){
for (int i=0;i<10000;i++){
Hehe.count();
}
}
Note: I'm new to english, so please forgive me for any wrong in it.
I use thread-local for save a resource per-thread; and use it(thread-local) in a some tasks. I run my task by a java executor-service. I would close my resources when a thread going to terminate; then i need run a task in all created threads by executor-service, after me call "executor.shoutdown" method. how i can force executor to run a task per-thread, when it would terminate those?
import java.util.concurrent.*;
public class Main2 {
public static void main(String[] args) {
ExecutorService executor = new ForkJoinPool(3);
SimpleValue val = new SimpleValue();
for(int i=0; i<1000; i++){
executor.execute(new Task(val));
}
executor.shutdown();
while( true ) {
try {
if( executor.awaitTermination(1, TimeUnit.SECONDS) ) System.exit(0);
} catch(InterruptedException intrExc) {
// continue...
}
}
}
protected static interface ResourceProvider<T>
extends AutoCloseable {
public T get();
public ResourceProvider<T> reset() throws Exception;
public ResourceProvider<T> reset(boolean force) throws Exception;
public void close();
}
protected static abstract class ThreadLocalResourceProvider<T>
extends ThreadLocal<T>
implements ResourceProvider<T> {}
protected static class SimpleValue
extends ThreadLocalResourceProvider<String> {
public String initialValue() {
return "Hello " + Thread.currentThread().getName();
}
public SimpleValue reset() throws Exception {
return reset(false);
}
public SimpleValue reset(boolean force) throws Exception{
set(this.initialValue());
return this;
}
public void close() {
remove();
}
}
protected static class Task
implements Runnable {
protected SimpleValue val;
public Task(SimpleValue val) {
this.val = val;
}
#Override
public void run() {
try {
System.out.print(val.reset().get());
} catch( Exception exc ) {
System.out.print( exc.getMessage() );
}
}
}
}
Most executors can be constructed with a ThreadFactory. That's also true for ForkJoinPool. However, for simplification, I use a different ExecutorService.
ExecutorService executor = Executors.newFixedThreadPool(
10, new FinalizerThreadFactory(Executors.defaultThreadFactory()));
The class FinalizerThreadFactory delegates the creation of threads to the passed thread factory. However, it creates threads that will execution some additional code before they exit. That's quite simple:
class FinalizerThreadFactory implements ThreadFactory {
private final ThreadFactory delegate;
public FinalizerThreadFactory(ThreadFactory delegate) {
this.delegate = delegate;
}
public Thread newThread(final Runnable r) {
return delegate.newThread(new Runnable() {
public void run() {
try {
r.run();
} finally {
// finalizer code goes here.
}
}
});
}
}