Skip to content

Instantly share code, notes, and snippets.

@mizdra
Created November 9, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizdra/4e60a917330e3afa861b to your computer and use it in GitHub Desktop.
Save mizdra/4e60a917330e3afa861b to your computer and use it in GitHub Desktop.
package synchronizedtest1;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SynchronizedTest1 {
public static void main(String[] args) {
A a = new A();
B b = new B(a);
a.start();
b.start();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(SynchronizedTest1.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
}
class A extends Thread {
private Object obj;
public A() {
obj = new Object();
}
public void printChar(char value) {
synchronized (obj) {
while (true)
System.out.println(value);
}
}
@Override
public void run() {
printChar('A');
}
}
class B extends Thread {
private A a;
public B(A a) {
this.a = a;
}
@Override
public void run() {
a.printChar('B');
}
}
package synchronizedtest2;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SynchronizedTest2 {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
a.start();
b.start();
c.start();
d.start();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(SynchronizedTest2.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
}
class A extends Thread {
public synchronized void printChar(char value) {
while (true)
System.out.println(value);
}
@Override
public void run() {
printChar('A');
}
}
class B extends Thread {
public static synchronized void printChar(char value) {
while (true)
System.out.println(value);
}
@Override
public void run() {
printChar('B');
}
}
class C extends Thread {
public void printChar(char value) {
synchronized (this) {
while (true)
System.out.println(value);
}
}
@Override
public void run() {
printChar('C');
}
}
class D extends Thread {
public static void printChar(char value) {
synchronized (D.class) {
while (true)
System.out.println(value);
}
}
@Override
public void run() {
printChar('D');
}
}
package synchronizedtest3;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SynchronizedTest3 {
public static void main(String[] args) {
A a = new A();
B b = new B(a);
C c = new C(a);
D d = new D();
a.start();
b.start();
c.start();
d.start();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(SynchronizedTest3.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
}
class A extends Thread {
public synchronized void printChar(char value) {
while (true)
System.out.println(value);
}
public synchronized void printString(String value) {
while (true)
System.out.println(value);
}
@Override
public void run() {
printChar('A');
}
}
class B extends Thread {
private A a;
public B(A a) {
this.a = a;
}
@Override
public void run() {
a.printChar('B');
}
}
class C extends Thread {
private A a;
public C(A a) {
this.a = a;
}
@Override
public void run() {
a.printString("CCC");
}
}
class D extends A {
@Override
public void run() {
printChar('D');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment