Last change
on this file since 14 was 2, checked in by Mattia Monga, 14 years ago |
Importazione sorgenti
|
File size:
1.0 KB
|
Rev | Line | |
---|
[2] | 1 | class ClasseAttiva extends Thread{
|
---|
| 2 | protected static int shared = 0;
|
---|
| 3 | public void run() {
|
---|
| 4 | while (true) {
|
---|
| 5 | if (shared >= 10) break;
|
---|
| 6 | try {
|
---|
| 7 | Thread.sleep(1000);
|
---|
| 8 | }
|
---|
| 9 | catch(InterruptedException e){
|
---|
| 10 | System.err.println(e);
|
---|
| 11 | }
|
---|
| 12 | if (shared >= 10){
|
---|
| 13 | throw new Error("Corsa critica!!!");
|
---|
| 14 | }
|
---|
| 15 | System.out.print(this.getName());
|
---|
| 16 | System.out.print(" s = ");
|
---|
| 17 | System.out.println(this.shared);
|
---|
| 18 | shared += 1;
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | public class Shared {
|
---|
| 26 | public static final void main(final String[] args) {
|
---|
| 27 | ClasseAttiva o1 = new ClasseAttiva();
|
---|
| 28 | ClasseAttiva o2 = new ClasseAttiva();
|
---|
| 29 | o1.start();
|
---|
| 30 | o2.start();
|
---|
| 31 | while (true){
|
---|
| 32 | try {
|
---|
| 33 | Thread.sleep(1000);
|
---|
| 34 | }
|
---|
| 35 | catch(InterruptedException e){
|
---|
| 36 | System.err.println(e);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | System.out.println("Main thread");
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.