This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private long counter = 0; | |
private final Object mutex = new Object(); | |
public void incrementCounter() { | |
synchronized (mutex) { | |
counter++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public synchronized void incrementCounter() { | |
counter++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private AtomicLong counter = new AtomicLong(); | |
public void incrementCounter() { | |
counter.incrementAndGet(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Object resource = new Object(); | |
public synchronized Object getResource() { | |
return resource; | |
} | |
public synchronized void setResource(Object resource) { | |
this.resource = resource; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Object resource = new Object(); | |
private AtomicReference<Object> reference = new AtomicReference<Object>(resource); | |
public Object getResource() { | |
return reference.get(); | |
} | |
public void setResource(Object resource) { | |
reference.set(resource); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("thread 1"); | |
} | |
}; | |
Thread thread1 = new Thread(runnable); | |
thread1.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thread thread2 = new Thread() { | |
@Override | |
public void run() { | |
System.out.println("thread 2"); | |
} | |
}; | |
thread2.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println(Thread.currentThread().getName()); | |
} | |
}; | |
executor.execute(runnable); | |
// executing once again |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExecutorService executor = Executors.newFixedThreadPool(2); | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println(Thread.currentThread().getName()); | |
} | |
}; | |
executor.execute(runnable); | |
executor.execute(runnable); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Resource { | |
private Object veryImportantObject = new Object(); | |
private final Object mutex = new Object(); | |
public synchronized Object getVeryImportantObject() { | |
System.out.println("Getting object"); | |
return veryImportantObject; | |
} |
OlderNewer