blob: e1cd8c2b24d6e438692d1eb6ddef570bf2c0b78b [file] [log] [blame]
Ian Zerny44c9e002017-09-12 09:23:12 +02001// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5public class SynchronizedBlock {
6
7 public static int emptyBlock(Object obj) {
8 int x = 123;
9 synchronized (obj) {}
10 int y = 456;
11 return x + y;
12 }
13
14 public static int nonThrowingBlock(Object obj) {
15 int x = 123;
16 synchronized (obj) {
17 dontThrow();
18 }
19 int y = 456;
20 return x + y;
21 }
22
23 public static int throwingBlock(Object obj) {
24 try {
25 int x = 123;
26 synchronized (obj) {
27 doThrow();
28 }
29 int y = 456;
30 return x + y;
31 } catch (Throwable e) { return 42; } // one line to avoid Java vs Art issue.
32 }
33
34 public static int nestedNonThrowingBlock(Object obj1, Object obj2) {
35 int x = 123;
36 synchronized (obj1) {
37 dontThrow();
38 synchronized (obj2) {
39 dontThrow();
40 }
41 }
42 int y = 456;
43 return x + y;
44 }
45
46 public static int nestedThrowingBlock(Object obj1, Object obj2) {
47 try {
48 int x = 123;
49 synchronized (obj1) {
50 dontThrow();
51 synchronized (obj2) {
52 doThrow();
53 }
54 }
55 int y = 456;
56 return x + y;
57 } catch (Throwable e) { return 42; } // one line to avoid Java vs Art issue.
58 }
59
60 public static void dontThrow() {
61 return;
62 }
63
64 public static void doThrow() {
65 throw new RuntimeException();
66 }
67
68 public static void main(String[] args) {
69 System.out.println(emptyBlock(new Object()));
70 System.out.println(nonThrowingBlock(new Object()));
71 System.out.println(throwingBlock(new Object()));
72 System.out.println(nestedNonThrowingBlock(new Object(), new Object()));
73 System.out.println(nestedThrowingBlock(new Object(), new Object()));
74 }
75}