Skip to content

Commit 1fd7173

Browse files
committed
классная работа
1 parent 8454038 commit 1fd7173

7 files changed

Lines changed: 210 additions & 3 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class Counter {
4+
private int c;
5+
6+
public Counter() {
7+
c = 0;
8+
}
9+
10+
public int value() {
11+
return c;
12+
}
13+
14+
public synchronized void inc() {
15+
c++;
16+
}
17+
18+
public synchronized void dec(){
19+
c--;
20+
}
21+
}

src/ru/geekbrains/java2/lesson_05/Classwork/Example_SB_1.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.geekbrains.java2.lesson_05;
1+
package ru.geekbrains.java2.lesson_05.Classwork;
22

33
public class Example_SB_1 {
44
public static void main(String[] args) {
@@ -13,7 +13,7 @@ public synchronized void method1() {
1313
for (int i = 0; i < 10; i++) {
1414
System.out.println(i);
1515
try {
16-
Thread.sleep(100);
16+
Thread.sleep(101);
1717
} catch (InterruptedException e) {
1818
e.printStackTrace();
1919
}
@@ -26,7 +26,7 @@ public synchronized void method2() {
2626
for (int i = 0; i < 10; i++) {
2727
System.out.println(i);
2828
try {
29-
Thread.sleep(100);
29+
Thread.sleep(102);
3030
} catch (InterruptedException e) {
3131
e.printStackTrace();
3232
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class Example_SB_2 {
4+
private Object lock1 = new Object ();
5+
public static void main ( String [] args ) {
6+
Example_SB_2 e2 = new Example_SB_2 ();
7+
System . out . println ( "Start" );
8+
new Thread (() -> e2 . method1 ()). start ();
9+
new Thread (() -> e2 . method1 ()). start ();
10+
}
11+
public void method1 () {
12+
System . out . println ( "Block-1 begin" );
13+
for ( int i = 100 ; i < 103 ; i ++) {
14+
System . out . println ( i );
15+
try {
16+
Thread . sleep ( 100 );
17+
} catch ( InterruptedException e ) {
18+
e . printStackTrace ();
19+
}
20+
}
21+
System . out . println ( "Block-1 end" );
22+
synchronized (lock1){
23+
System.out.println("Synch block begin");
24+
for (int i = 0; i < 10; i++) {
25+
System.out.println(i);
26+
try {
27+
Thread.sleep(103);
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
System.out.println("Synch block end");
33+
}
34+
System . out . println ( "M2" );
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class Example_SB_3 {
4+
public static void main(String[] args) {
5+
System.out.println("Start");
6+
new Thread(() -> method()).start();
7+
new Thread(() -> method()).start();
8+
}
9+
10+
public synchronized static void method() { // синхронизация по классу
11+
for (int i = 0; i < 10; i++) {
12+
System.out.println(i + " " + Thread.currentThread().getName());
13+
try {
14+
Thread.sleep(100);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
}
20+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class MainClass {
4+
public static void main(String[] args) {
5+
/*System.out.println(Thread.currentThread().getName());
6+
7+
MyThread myT1 = new MyThread();
8+
MyThread myT2 = new MyThread();
9+
myT1.start();
10+
myT2.start();
11+
12+
Thread myR3 = new Thread(new MyRunnable());
13+
Thread myR4 = new Thread(new MyRunnable());
14+
myR3.start();
15+
myR4.start();
16+
try {
17+
Thread.sleep(200);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
System.out.println("end");*/
22+
23+
/* Thread th1 = new Thread(new Runnable() {
24+
@Override
25+
public void run() {
26+
for (int i = 1; i < 6; i++) {
27+
System.out.println(i);
28+
}
29+
}
30+
});
31+
32+
Thread th2 = new Thread(new Runnable() {
33+
@Override
34+
public void run() {
35+
for (int i = 11; i < 16; i++) {
36+
System.out.println(i);
37+
}
38+
}
39+
});
40+
41+
th1.start();
42+
th2.start();
43+
44+
try {
45+
th1.join();
46+
th2.join();
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
System.out.println("end");*/
51+
52+
/* Thread th = new Thread(new Runnable() {
53+
@Override
54+
public void run() {
55+
int time = 0;
56+
while (true){
57+
time++;
58+
try {
59+
Thread.sleep(1000);
60+
} catch (InterruptedException e) {
61+
e.printStackTrace();
62+
}
63+
System.out.println(time);
64+
}
65+
}
66+
});
67+
th.setDaemon(true);
68+
th.start();
69+
70+
try {
71+
Thread.sleep(5000);
72+
} catch (InterruptedException e) {
73+
e.printStackTrace();
74+
}
75+
System.out.println("end");*/
76+
77+
Counter counter = new Counter();
78+
79+
Thread th1 = new Thread(new Runnable() {
80+
@Override
81+
public void run() {
82+
for (int i = 0; i < 100; i++) {
83+
counter.inc();
84+
}
85+
}
86+
});
87+
88+
Thread th2 = new Thread(new Runnable() {
89+
@Override
90+
public void run() {
91+
for (int i = 0; i < 100; i++) {
92+
counter.dec();
93+
}
94+
}
95+
});
96+
97+
th1.start();
98+
th2.start();
99+
100+
try {
101+
th1.join();
102+
th2.join();
103+
} catch (InterruptedException e) {
104+
e.printStackTrace();
105+
}
106+
107+
System.out.println(counter.value());
108+
}
109+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class MyRunnable implements Runnable {
4+
@Override
5+
public void run() {
6+
for (int i = 0; i < 10; i++) {
7+
System.out.println(i + " " + Thread.currentThread().getName());
8+
}
9+
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.geekbrains.java2.lesson_05.Classwork;
2+
3+
public class MyThread extends Thread {
4+
@Override
5+
public void run() {
6+
for (int i = 0; i < 10; i++) {
7+
System.out.println(i + " " + Thread.currentThread().getName());
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)