Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static class DoSmth{
- private final MyClass a;
- private final MyClass b;
- public DoSmth(){
- a = new MyClass("a");
- b = new MyClass("b");
- }
- private void firstThread(){
- synchronized (a){
- b.print();
- }
- }
- private void secondThread(){
- synchronized (b){
- a.print();
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- DoSmth ds = new DoSmth();
- Thread thread1 = new Thread(new Runnable() {
- @Override
- public void run() {
- ds.firstThread();
- }
- });
- Thread thread2 = new Thread(new Runnable() {
- @Override
- public void run() {
- ds.secondThread();
- }
- });
- thread1.start();
- thread2.start();
- thread1.join();
- thread2.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement