franta-hg@21
|
1 |
/**
|
franta-hg@21
|
2 |
* RAII v Javě
|
franta-hg@21
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@21
|
4 |
*
|
franta-hg@21
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@21
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@21
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@21
|
8 |
* (at your option) any later version.
|
franta-hg@21
|
9 |
*
|
franta-hg@21
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@21
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@21
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@21
|
13 |
* GNU General Public License for more details.
|
franta-hg@21
|
14 |
*
|
franta-hg@21
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@21
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@21
|
17 |
*/
|
franta-hg@21
|
18 |
package cz.frantovo.priklady.raii;
|
franta-hg@21
|
19 |
|
franta-hg@21
|
20 |
/**
|
franta-hg@21
|
21 |
*
|
franta-hg@21
|
22 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@21
|
23 |
*/
|
franta-hg@21
|
24 |
public class RAIIDemo {
|
franta-hg@21
|
25 |
|
franta-hg@21
|
26 |
public static void main(String[] args) {
|
franta-hg@21
|
27 |
|
franta-hg@21
|
28 |
/**
|
franta-hg@21
|
29 |
* Bez výjimek:
|
franta-hg@21
|
30 |
*/
|
franta-hg@21
|
31 |
try {
|
franta-hg@21
|
32 |
try (Zdroj a = new Zdroj("a", false, false, false)) {
|
franta-hg@21
|
33 |
try (Zdroj b = new Zdroj("b", false, false, false)) {
|
franta-hg@21
|
34 |
try (Zdroj c = new Zdroj("c", false, false, false)) {
|
franta-hg@21
|
35 |
a.něcoDělej();
|
franta-hg@21
|
36 |
b.něcoDělej();
|
franta-hg@21
|
37 |
c.něcoDělej();
|
franta-hg@21
|
38 |
} // končí rozsah platnosti proměnné c → volá se c.close()
|
franta-hg@21
|
39 |
} // b.close()
|
franta-hg@21
|
40 |
} // a.close()
|
franta-hg@21
|
41 |
|
franta-hg@21
|
42 |
} catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) {
|
franta-hg@24
|
43 |
vypišChybu(e);
|
franta-hg@21
|
44 |
}
|
franta-hg@21
|
45 |
|
franta-hg@21
|
46 |
System.err.println("--------------------------------");
|
franta-hg@21
|
47 |
|
franta-hg@21
|
48 |
/**
|
franta-hg@21
|
49 |
* S výjimkami:
|
franta-hg@21
|
50 |
*/
|
franta-hg@21
|
51 |
try {
|
franta-hg@21
|
52 |
try (Zdroj a = new Zdroj("a", false, false, true)) {
|
franta-hg@21
|
53 |
try (Zdroj b = new Zdroj("b", false, false, true)) {
|
franta-hg@21
|
54 |
try (Zdroj c = new Zdroj("c", false, true, true)) {
|
franta-hg@21
|
55 |
a.něcoDělej();
|
franta-hg@21
|
56 |
b.něcoDělej();
|
franta-hg@23
|
57 |
c.něcoDělej();
|
franta-hg@21
|
58 |
}
|
franta-hg@21
|
59 |
}
|
franta-hg@21
|
60 |
}
|
franta-hg@21
|
61 |
} catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) {
|
franta-hg@24
|
62 |
vypišChybu(e);
|
franta-hg@21
|
63 |
}
|
franta-hg@21
|
64 |
|
franta-hg@21
|
65 |
System.gc(); // aby se GC vůbec stihl spustit
|
franta-hg@21
|
66 |
|
franta-hg@21
|
67 |
čekej(); // aby GC stihl zavolat finalize()
|
franta-hg@21
|
68 |
|
franta-hg@21
|
69 |
}
|
franta-hg@21
|
70 |
|
franta-hg@24
|
71 |
private static void vypišChybu(final cz.frantovo.priklady.raii.Chyba e) {
|
franta-hg@24
|
72 |
System.err.println(e);
|
franta-hg@24
|
73 |
for (Throwable t : e.getSuppressed()) {
|
franta-hg@24
|
74 |
System.err.println("\t" + t + " (potlačená výjimka)");
|
franta-hg@24
|
75 |
}
|
franta-hg@24
|
76 |
}
|
franta-hg@24
|
77 |
|
franta-hg@21
|
78 |
private static void čekej() {
|
franta-hg@21
|
79 |
try {
|
franta-hg@21
|
80 |
Thread.sleep(100);
|
franta-hg@21
|
81 |
} catch (InterruptedException ex) {
|
franta-hg@21
|
82 |
}
|
franta-hg@21
|
83 |
}
|
franta-hg@21
|
84 |
}
|