├── .gitignore
├── README.md
├── build.xml
├── manifest.mf
├── nbproject
├── build-impl.xml
├── genfiles.properties
├── project.properties
└── project.xml
└── src
├── Assignment2_TestFiles
├── MyThread1.java
└── MyThread2.java
├── Test_files
├── Assignment1
│ ├── MySleep.java
│ ├── MyThread.java
│ ├── TestWaitNotify.java
│ └── ThreadBlocked.java
├── Assignment2
│ ├── MyThread1.java
│ └── MyThread2.java
├── GroupProject
│ └── ListGithubAccounts.txt
└── Issue_01
│ └── HelloWorld.java
├── Week_01
├── MySleep.java
├── MyThread.java
└── MyThreadThread.java
├── Week_02
├── MainThread.java
├── MyDaemonThread.java
├── MyRunnable.java
├── MyThread.java
├── RunMethod.java
└── ThreadTwice.java
├── Week_03
├── CheckAlive.java
├── CheckState.java
├── CheckStateAlive.java
├── ThreadAlive.java
├── ThreadBlocked.java
├── ThreadNew.java
├── ThreadRunnable.java
├── ThreadStates.java
├── ThreadStatesInJava.java
├── ThreadTerminated.java
├── ThreadTimedWaiting.java
└── ThreadWaiting.java
├── Week_04
├── Homework_WaitNotify.java
├── TestInterrupt1.java
├── TestInterrupt2.java
├── TestInterrupt3.java
├── TestJoin1.java
├── TestPriority1.java
├── TestPriority2.java
├── TestPriority3.java
├── TestSleep1.java
├── TestSleep2.java
├── TestYield1.java
└── TestYield2.java
├── Week_05
├── Images
│ ├── Atomic-Classes.png
│ ├── Atomic-Operation.png
│ ├── Common-Atomic-Methods.png
│ └── Thread-Unsafe.png
├── MyExecuter.java
├── SheepManager1p.java
├── SheepManager1s1.java
├── SheepManager1s2.java
├── TestAtomicInteger1p.java
└── TestAtomicInteger1s.java
├── Week_06
├── TestSynchronized1.java
├── TestSynchronized2p.java
├── TestSynchronized2s1.java
└── TestSynchronizedCounter.java
├── Week_07
├── PleaseRead.txt
├── StudentLibrary
│ ├── Book.java
│ ├── Constants.java
│ ├── Student.java
│ └── TestApp.java
├── TestLock1.java
├── TestLock1p.java
└── TestLock1s.java
├── Week_08
├── TestStopThread1.java
├── TestStopThread2.java
├── TestTerminated1.java
└── TestVolatile1.java
├── Week_09
└── test.txt
└── Week_13
├── ParallelSum.java
├── ParallelWorker.java
├── SequentialSum.java
├── TestParallel.java
├── TestRecursiveAction1.java
├── TestRecursiveTask1.java
└── TestSequential.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /nbproject/private/
2 | /build/
3 | /dist/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java Concurrency
2 |
3 | ### Main Reference:
4 | https://docs.oracle.com/javase/tutorial/essential/concurrency/
5 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | Builds, tests, and runs the project STIW3054-RT-Programming.
12 |
13 |
73 |
74 |
--------------------------------------------------------------------------------
/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | X-COMMENT: Main-Class will be added automatically by build
3 |
4 |
--------------------------------------------------------------------------------
/nbproject/build-impl.xml:
--------------------------------------------------------------------------------
1 |
2 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 | Must set src.dir
234 | Must set test.src.dir
235 | Must set build.dir
236 | Must set dist.dir
237 | Must set build.classes.dir
238 | Must set dist.javadoc.dir
239 | Must set build.test.classes.dir
240 | Must set build.test.results.dir
241 | Must set build.classes.excludes
242 | Must set dist.jar
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 | Must set javac.includes
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 | No tests executed.
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 | Must set JVM to use for profiling in profiler.info.jvm
723 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
724 |
725 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
891 |
892 |
893 |
894 |
895 |
896 |
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 | Must select some files in the IDE or set javac.includes
952 |
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
965 |
966 |
967 |
968 |
969 |
970 |
971 |
972 |
973 |
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 |
996 |
997 |
998 |
999 |
1000 |
1001 | To run this application from the command line without Ant, try:
1002 |
1003 | java -jar "${dist.jar.resolved}"
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1039 |
1040 |
1041 |
1042 |
1043 |
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 | Must select one file in the IDE or set run.class
1051 |
1052 |
1053 |
1054 | Must select one file in the IDE or set run.class
1055 |
1056 |
1057 |
1062 |
1063 |
1064 |
1065 |
1066 |
1067 |
1068 |
1069 |
1070 |
1071 |
1072 |
1073 |
1074 |
1075 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 | Must select one file in the IDE or set debug.class
1082 |
1083 |
1084 |
1085 |
1086 | Must select one file in the IDE or set debug.class
1087 |
1088 |
1089 |
1090 |
1091 | Must set fix.includes
1092 |
1093 |
1094 |
1095 |
1096 |
1097 |
1098 |
1103 |
1106 |
1107 | This target only works when run from inside the NetBeans IDE.
1108 |
1109 |
1110 |
1111 |
1112 |
1113 |
1114 |
1115 |
1116 | Must select one file in the IDE or set profile.class
1117 | This target only works when run from inside the NetBeans IDE.
1118 |
1119 |
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 | This target only works when run from inside the NetBeans IDE.
1127 |
1128 |
1129 |
1130 |
1131 |
1132 |
1133 |
1134 |
1135 |
1136 |
1137 |
1138 |
1139 | This target only works when run from inside the NetBeans IDE.
1140 |
1141 |
1142 |
1143 |
1144 |
1145 |
1146 |
1147 |
1148 |
1149 |
1150 |
1151 |
1152 |
1153 |
1154 |
1155 |
1156 |
1157 |
1158 |
1159 |
1160 |
1161 |
1164 |
1165 |
1166 |
1167 |
1168 |
1169 |
1170 |
1171 |
1172 |
1173 |
1174 |
1175 |
1176 |
1177 | Must select one file in the IDE or set run.class
1178 |
1179 |
1180 |
1181 |
1182 |
1183 | Must select some files in the IDE or set test.includes
1184 |
1185 |
1186 |
1187 |
1188 | Must select one file in the IDE or set run.class
1189 |
1190 |
1191 |
1192 |
1193 | Must select one file in the IDE or set applet.url
1194 |
1195 |
1196 |
1197 |
1202 |
1203 |
1204 |
1205 |
1206 |
1207 |
1208 |
1209 |
1210 |
1211 |
1212 |
1213 |
1214 |
1215 |
1216 |
1217 |
1218 |
1219 |
1220 |
1221 |
1222 |
1223 |
1224 |
1225 |
1226 |
1227 |
1228 |
1229 |
1230 |
1231 |
1232 |
1233 |
1234 |
1235 |
1236 |
1237 |
1238 |
1239 |
1240 |
1241 |
1246 |
1247 |
1248 |
1249 |
1250 |
1251 |
1252 |
1253 |
1254 |
1255 |
1256 |
1257 |
1258 |
1259 |
1260 |
1261 |
1262 |
1263 |
1264 |
1265 |
1266 |
1267 |
1268 |
1269 |
1270 |
1271 |
1272 | Must select some files in the IDE or set javac.includes
1273 |
1274 |
1275 |
1276 |
1277 |
1278 |
1279 |
1280 |
1281 |
1282 |
1283 |
1284 |
1289 |
1290 |
1291 |
1292 |
1293 |
1294 |
1295 |
1296 | Some tests failed; see details above.
1297 |
1298 |
1299 |
1300 |
1301 |
1302 |
1303 |
1304 |
1305 | Must select some files in the IDE or set test.includes
1306 |
1307 |
1308 |
1309 | Some tests failed; see details above.
1310 |
1311 |
1312 |
1313 | Must select some files in the IDE or set test.class
1314 | Must select some method in the IDE or set test.method
1315 |
1316 |
1317 |
1318 | Some tests failed; see details above.
1319 |
1320 |
1321 |
1326 |
1327 | Must select one file in the IDE or set test.class
1328 |
1329 |
1330 |
1331 | Must select one file in the IDE or set test.class
1332 | Must select some method in the IDE or set test.method
1333 |
1334 |
1335 |
1336 |
1337 |
1338 |
1339 |
1340 |
1341 |
1342 |
1343 |
1344 |
1349 |
1350 | Must select one file in the IDE or set applet.url
1351 |
1352 |
1353 |
1354 |
1355 |
1356 |
1357 |
1362 |
1363 | Must select one file in the IDE or set applet.url
1364 |
1365 |
1366 |
1367 |
1368 |
1369 |
1370 |
1371 |
1376 |
1377 |
1378 |
1379 |
1380 |
1381 |
1382 |
1383 |
1384 |
1385 |
1386 |
1387 |
1388 |
1389 |
1390 |
1391 |
1392 |
1393 |
1394 |
1395 |
1396 |
1397 |
1398 |
1399 |
1400 |
1401 |
1402 |
1403 |
1404 |
1405 |
1406 |
1407 |
1408 |
1409 |
1410 |
1411 |
1412 |
1413 |
1414 |
1415 |
1416 |
1417 |
1418 |
1419 |
1420 |
1421 |
--------------------------------------------------------------------------------
/nbproject/genfiles.properties:
--------------------------------------------------------------------------------
1 | build.xml.data.CRC32=e54b5b54
2 | build.xml.script.CRC32=0c70e84c
3 | build.xml.stylesheet.CRC32=8064a381@1.80.1.48
4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6 | nbproject/build-impl.xml.data.CRC32=e54b5b54
7 | nbproject/build-impl.xml.script.CRC32=08637068
8 | nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
9 |
--------------------------------------------------------------------------------
/nbproject/project.properties:
--------------------------------------------------------------------------------
1 | annotation.processing.enabled=true
2 | annotation.processing.enabled.in.editor=false
3 | annotation.processing.processors.list=
4 | annotation.processing.run.all.processors=true
5 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
6 | application.title=STIW3054-RT-Programming
7 | application.vendor=zhamricheani
8 | build.classes.dir=${build.dir}/classes
9 | build.classes.excludes=**/*.java,**/*.form
10 | # This directory is removed when the project is cleaned:
11 | build.dir=build
12 | build.generated.dir=${build.dir}/generated
13 | build.generated.sources.dir=${build.dir}/generated-sources
14 | # Only compile against the classpath explicitly listed here:
15 | build.sysclasspath=ignore
16 | build.test.classes.dir=${build.dir}/test/classes
17 | build.test.results.dir=${build.dir}/test/results
18 | # Uncomment to specify the preferred debugger connection transport:
19 | #debug.transport=dt_socket
20 | debug.classpath=\
21 | ${run.classpath}
22 | debug.test.classpath=\
23 | ${run.test.classpath}
24 | # Files in build.classes.dir which should be excluded from distribution jar
25 | dist.archive.excludes=
26 | # This directory is removed when the project is cleaned:
27 | dist.dir=dist
28 | dist.jar=${dist.dir}/STIW3054-RT-Programming.jar
29 | dist.javadoc.dir=${dist.dir}/javadoc
30 | endorsed.classpath=
31 | excludes=
32 | includes=**
33 | jar.compress=false
34 | javac.classpath=
35 | # Space-separated list of extra javac options
36 | javac.compilerargs=
37 | javac.deprecation=false
38 | javac.external.vm=true
39 | javac.processorpath=\
40 | ${javac.classpath}
41 | javac.source=1.8
42 | javac.target=1.8
43 | javac.test.classpath=\
44 | ${javac.classpath}:\
45 | ${build.classes.dir}
46 | javac.test.processorpath=\
47 | ${javac.test.classpath}
48 | javadoc.additionalparam=
49 | javadoc.author=false
50 | javadoc.encoding=${source.encoding}
51 | javadoc.noindex=false
52 | javadoc.nonavbar=false
53 | javadoc.notree=false
54 | javadoc.private=false
55 | javadoc.splitindex=true
56 | javadoc.use=true
57 | javadoc.version=false
58 | javadoc.windowtitle=
59 | manifest.file=manifest.mf
60 | meta.inf.dir=${src.dir}/META-INF
61 | mkdist.disabled=false
62 | platform.active=default_platform
63 | run.classpath=\
64 | ${javac.classpath}:\
65 | ${build.classes.dir}
66 | # Space-separated list of JVM arguments used when running the project.
67 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
68 | # To set system properties for unit tests define test-sys-prop.name=value:
69 | run.jvmargs=
70 | run.test.classpath=\
71 | ${javac.test.classpath}:\
72 | ${build.test.classes.dir}
73 | source.encoding=UTF-8
74 | src.dir=src
75 | test.src.dir=test
76 |
--------------------------------------------------------------------------------
/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 | STIW3054-RT-Programming
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/Assignment2_TestFiles/MyThread1.java:
--------------------------------------------------------------------------------
1 | //Semester: #A171
2 | //Course: #STIW3054
3 | //Group: #A
4 | //Task: #Assignment1
5 | //Matrik: #898989
6 | //Name: #Lin Dan
7 |
8 | package Assignment2_TestFiles;
9 |
10 | class MyThread1 extends Thread {
11 |
12 | public void run() {
13 | System.out.println("running...");
14 | }
15 |
16 | public static void main(String args[]) {
17 | MyThread1 t1 = new MyThread1();
18 | System.out.println("Name of t1:" + t1.getName());
19 | System.out.println("id of t1:" + t1.getId());
20 | t1.start();
21 | t1.setName("STIW3054");
22 | System.out.println("After changing name of t1:" + t1.getName());
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Assignment2_TestFiles/MyThread2.java:
--------------------------------------------------------------------------------
1 | //Semester: #A171
2 | //Course: #STIW3054
3 | //Group: #A
4 | //Task: #Assignment1
5 | //Matrik: #123456
6 | //Name: #Ali bin Abu Bakar
7 |
8 | package Assignment2_TestFiles;
9 |
10 | class MyThread2 extends Thread {
11 |
12 | public void run() {
13 | System.out.println("running...");
14 | }
15 |
16 | public static void main(String args[]) {
17 | MyThread2 t1 = new MyThread2();
18 | MyThread2 t2 = new MyThread2();
19 | System.out.println("Name of t1:" + t1.getName());
20 | System.out.println("Name of t2:" + t2.getName());
21 | System.out.println("id of t1:" + t1.getId());
22 |
23 | //How to start thread
24 | t1.start();
25 | t2.start();
26 |
27 | t1.setName("STIW3054");
28 | System.out.println("After changing name of t1:" + t1.getName());
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Test_files/Assignment1/MySleep.java:
--------------------------------------------------------------------------------
1 | package Test_files.Assignment1;
2 |
3 | public class MySleep extends Thread {
4 |
5 | public static void main(String[] args) {
6 | new Thread(new MySleep()).start();
7 | }
8 |
9 | @Override
10 | public void run() {
11 | try {
12 | for (int x = 0; x < 1000; x++) {
13 | System.out.println(x);
14 | sleep(2000);
15 | }
16 | } catch (Exception e) {
17 | e.printStackTrace();
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Test_files/Assignment1/MyThread.java:
--------------------------------------------------------------------------------
1 | package Test_files.Assignment1;
2 |
3 | class MyThread extends Thread {
4 |
5 | boolean waiting = true;
6 | boolean ready = false;
7 |
8 | @Override
9 | public void run() {
10 | String tName = Thread.currentThread().getName();
11 | System.out.println(tName + " starting.");
12 |
13 | while (waiting) {
14 | System.out.println("waiting:" + waiting);
15 | }
16 | System.out.println("waiting...");
17 | startWait();
18 | try {
19 | Thread.sleep(1000);
20 | } catch (Exception exc) {
21 | System.out.println(tName + " interrupted.");
22 | }
23 | System.out.println(tName + " terminating.");
24 | }
25 |
26 | synchronized void startWait() {
27 | try {
28 | while (!ready) {
29 | wait();
30 | }
31 | } catch (InterruptedException exc) {
32 | System.out.println("wait() interrupted");
33 | }
34 | }
35 |
36 | synchronized void notice() {
37 | ready = true;
38 | notify();
39 | }
40 | }
41 |
42 |
--------------------------------------------------------------------------------
/src/Test_files/Assignment1/TestWaitNotify.java:
--------------------------------------------------------------------------------
1 | package Test_files.Assignment1;
2 |
3 | public class TestWaitNotify {
4 |
5 | public static void main(String args[]) throws Exception {
6 | MyThread thrd = new MyThread();
7 | thrd.setName("MyThread #1");
8 | showThreadStatus(thrd);
9 |
10 | thrd.start();
11 | Thread.sleep(50);
12 | showThreadStatus(thrd);
13 |
14 | thrd.waiting = false;
15 | Thread.sleep(50);
16 | showThreadStatus(thrd);
17 |
18 | thrd.notice();
19 | Thread.sleep(50);
20 | showThreadStatus(thrd);
21 |
22 | while (thrd.isAlive()) {
23 | System.out.println("alive");
24 | }
25 | showThreadStatus(thrd);
26 | }
27 |
28 | static void showThreadStatus(Thread thrd) {
29 | System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Test_files/Assignment1/ThreadBlocked.java:
--------------------------------------------------------------------------------
1 | package Test_files.Assignment1;
2 |
3 | class Shared {
4 |
5 | synchronized void methodOne(Shared s) {
6 | try {
7 | Thread.sleep(2000);
8 | } catch (InterruptedException e) {
9 | e.printStackTrace();
10 | }
11 | s.methodTwo(this);
12 | }
13 |
14 | synchronized void methodTwo(Shared s) {
15 | try {
16 | Thread.sleep(2000);
17 | } catch (InterruptedException e) {
18 | e.printStackTrace();
19 | }
20 | s.methodOne(this);
21 | }
22 | }
23 |
24 | class ThreadBlocked {
25 |
26 | public static void main(String[] args) {
27 |
28 | final Shared s1 = new Shared();
29 | final Shared s2 = new Shared();
30 |
31 | Thread t1 = new Thread() {
32 | @Override
33 | public void run() {
34 | s1.methodOne(s2);
35 | }
36 | };
37 |
38 | Thread t2 = new Thread() {
39 | @Override
40 | public void run() {
41 | s2.methodTwo(s1);
42 | }
43 | };
44 |
45 | t1.start();
46 | t2.start();
47 |
48 | try {
49 | Thread.sleep(3000);
50 | } catch (InterruptedException e) {
51 | e.printStackTrace();
52 | }
53 |
54 | System.out.println("state t1 = " + t1.getState());
55 | System.out.println("state t2 = " + t2.getState());
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/Test_files/Assignment2/MyThread1.java:
--------------------------------------------------------------------------------
1 | //Semester: #A171
2 | //Course: #STIW3054
3 | //Group: #A
4 | //Task: #Assignment1
5 | //Matrik: #898989
6 | //Name: #Lin Dan
7 |
8 | package Test_files.Assignment2;
9 |
10 | class MyThread1 extends Thread {
11 |
12 | public void run() {
13 | System.out.println("running...");
14 | }
15 |
16 | public static void main(String args[]) {
17 | MyThread1 t1 = new MyThread1();
18 | System.out.println("Name of t1:" + t1.getName());
19 | System.out.println("id of t1:" + t1.getId());
20 | t1.start();
21 | t1.setName("STIW3054");
22 | System.out.println("After changing name of t1:" + t1.getName());
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Test_files/Assignment2/MyThread2.java:
--------------------------------------------------------------------------------
1 | //Semester: #A171
2 | //Course: #STIW3054
3 | //Group: #A
4 | //Task: #Assignment1
5 | //Matrik: #123456
6 | //Name: #Ali bin Abu Bakar
7 |
8 | package Test_files.Assignment2;
9 |
10 | class MyThread2 extends Thread {
11 |
12 | public void run() {
13 | System.out.println("running...");
14 | }
15 |
16 | public static void main(String args[]) {
17 | MyThread2 t1 = new MyThread2();
18 | MyThread2 t2 = new MyThread2();
19 | System.out.println("Name of t1:" + t1.getName());
20 | System.out.println("Name of t2:" + t2.getName());
21 | System.out.println("id of t1:" + t1.getId());
22 |
23 | //How to start thread
24 | t1.start();
25 | t2.start();
26 |
27 | t1.setName("STIW3054");
28 | System.out.println("After changing name of t1:" + t1.getName());
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Test_files/GroupProject/ListGithubAccounts.txt:
--------------------------------------------------------------------------------
1 | https://github.com/AminAsyraf94/232491
2 | https://github.com/Meenanbeega96/237506
3 | https://github.com/DonLeeHoi/237768
4 | https://github.com/amirah16/238417
5 | https://github.com/goaykailing/240359
--------------------------------------------------------------------------------
/src/Test_files/Issue_01/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package Test_files.Issue_01;
2 |
3 | public class HelloWorld {
4 |
5 | public static void main(String[] args) {
6 | System.out.println("Hello SITW3054");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Week_01/MySleep.java:
--------------------------------------------------------------------------------
1 | package Week_01;
2 |
3 | public class MySleep extends Thread {
4 |
5 | public static void main(String[] args) {
6 | new Thread(new MySleep()).start();
7 | }
8 |
9 | @Override
10 | public void run() {
11 | try {
12 | for (int x = 0; x < 1000; x++) {
13 | System.out.println(x);
14 | sleep(2000);
15 | }
16 | } catch (Exception e) {
17 | e.printStackTrace();
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Week_01/MyThread.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_01;
3 |
4 | class MyThread extends Thread {
5 |
6 | public void run() {
7 | System.out.println("running...");
8 | }
9 |
10 | public static void main(String args[]) {
11 | MyThread t1 = new MyThread();
12 | MyThread t2 = new MyThread();
13 | System.out.println("Name of t1:" + t1.getName());
14 | System.out.println("Name of t2:" + t2.getName());
15 | System.out.println("id of t1:" + t1.getId());
16 |
17 | t1.start();
18 | t2.start();
19 |
20 | t1.setName("STIW3054");
21 | System.out.println("After changing name of t1:" + t1.getName());
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Week_01/MyThreadThread.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_01;
3 |
4 | public class MyThreadThread extends Thread {
5 |
6 | public static void main(String[] args) {
7 | new Thread(new MySleep()).start();
8 | new Thread(new MySleep()).start();
9 | }
10 |
11 | @Override
12 | public void run() {
13 | try {
14 | for (int x = 0; x < 1000; x++) {
15 | System.out.println(x);
16 | sleep(2000);
17 | }
18 | } catch (Exception e) {
19 | e.printStackTrace();
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/Week_02/MainThread.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class MainThread {
4 |
5 | public static void main(String args[]) {
6 | System.out.println("Current thread is: " + Thread.currentThread().getName());
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Week_02/MyDaemonThread.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class MyDaemonThread extends Thread {
4 |
5 | @Override
6 | public void run() {
7 |
8 | if (Thread.currentThread().isDaemon()) {
9 | System.out.println("Daemon thread executing ...");
10 | } else {
11 | System.out.println("user thread executing ...");
12 | }
13 | }
14 |
15 | public static void main(String[] args) {
16 |
17 | MyDaemonThread t1 = new MyDaemonThread();
18 | MyDaemonThread t2 = new MyDaemonThread();
19 |
20 | t1.setDaemon(true);
21 |
22 | t1.start();
23 | t2.start();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Week_02/MyRunnable.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class MyRunnable implements Runnable {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("thread is running...");
8 | }
9 |
10 | public static void main(String args[]) {
11 | MyRunnable m1 = new MyRunnable();
12 | Thread t1 = new Thread(m1);
13 | t1.start();
14 | }
15 | }
--------------------------------------------------------------------------------
/src/Week_02/MyThread.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class MyThread extends Thread {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("thread is running...");
8 | }
9 |
10 | public static void main(String args[]) {
11 | MyThread t1 = new MyThread();
12 | t1.start();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Week_02/RunMethod.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class RunMethod implements Runnable {
4 |
5 | @Override
6 | public void run() {
7 | for (int i = 1; i <= 10; i++) {
8 | try {
9 | Thread.sleep(500);
10 | } catch (InterruptedException ie) {
11 | ie.printStackTrace();
12 | }
13 | System.out.println(i);
14 | }
15 | }
16 |
17 | public static void main(String args[]) {
18 | Thread t1 = new Thread(new RunMethod(), "RT susah");
19 | Thread t2 = new Thread(new RunMethod(), "RT senang");
20 | t1.run();
21 | t2.run();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Week_02/ThreadTwice.java:
--------------------------------------------------------------------------------
1 | package Week_02;
2 |
3 | class Testing implements Runnable {
4 |
5 | @Override
6 | public void run() {
7 | Thread t = Thread.currentThread();
8 | System.out.println(t.getName() + " is running ...");
9 |
10 | }
11 |
12 | public static void main(String args[]) {
13 | Thread t1 = new Thread(new Testing(), "t1");
14 | t1.start();
15 | t1.start();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Week_03/CheckAlive.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class CheckAlive implements Runnable {
4 |
5 | public void run() {
6 | System.out.println(Thread.currentThread().getName() + " is running ...");
7 | }
8 |
9 | public static void main(String[] args) {
10 |
11 | Thread t1 = new Thread(new CheckAlive(), "FirstThread");
12 | Thread t2 = new Thread(new CheckAlive(), "SecondThread");
13 |
14 | System.out.println("State FirstThread: " + t1.getState());
15 | System.out.println("State SecondThread: " + t2.getState());
16 | t1.start();
17 | System.out.println("State FirstThread: " + t1.getState());
18 | System.out.println("State SecondThread: " + t2.getState());
19 |
20 | if (t1.isAlive()) {
21 | System.out.format("%s is alive.%n", t1.getName());
22 | } else {
23 | System.out.format("%s is not alive.%n", t1.getName());
24 | }
25 | System.out.println("State FirstThread: " + t1.getState());
26 | System.out.println("State SecondThread: " + t2.getState());
27 |
28 | if (t2.isAlive()) {
29 | System.out.format("%s is alive.%n", t2.getName());
30 | } else {
31 | System.out.format("%s is not alive.%n", t2.getName());
32 | }
33 | System.out.println("State FirstThread: " + t1.getState());
34 | System.out.println("State SecondThread: " + t2.getState());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Week_03/CheckState.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class GetStateRunnable implements Runnable {
4 |
5 | @Override
6 | public void run() {
7 | for (int i = 1; i <= 3; i++) {
8 | System.out.println("Run by " + Thread.currentThread().getName());
9 | try {
10 | Thread.sleep(1000);
11 | } catch (InterruptedException ex) {
12 | ex.printStackTrace();
13 | }
14 | }
15 | System.out.println("Thread State of " + Thread.currentThread().getName() + ": " + Thread.currentThread().getState());
16 | System.out.println("Exit of Thread: " + Thread.currentThread().getName());
17 | }
18 | }
19 |
20 | class CheckState {
21 |
22 | public static void main(String[] args) throws Exception {
23 | GetStateRunnable gsr = new GetStateRunnable();
24 | Thread kedah = new Thread(gsr);
25 | kedah.setName("Kedah");
26 | Thread perlis = new Thread(gsr);
27 | perlis.setName("Perlis");
28 | Thread johor = new Thread(gsr);
29 | johor.setName("Johor");
30 | System.out.println("Thread State of Kedah before start(): " + kedah.getState());
31 | System.out.println("Thread State of Kedah before start(): " + perlis.getState());
32 | System.out.println("Thread State of Kedah before start(): " + johor.getState());
33 | kedah.start();
34 | perlis.start();
35 | johor.start();
36 | System.out.println("Thread State of Kedah in Main method before sleep(): " + kedah.getState());
37 | System.out.println("Thread State of Perlis in Main method before sleep(): " + perlis.getState());
38 | System.out.println("Thread State of Johor in Main method before sleep(): " + johor.getState());
39 | Thread.sleep(5000);
40 | System.out.println("Thread State of Kedah in Main method after sleep(): " + kedah.getState());
41 | System.out.println("Thread State of Perlis in Main method after sleep(): " + perlis.getState());
42 | System.out.println("Thread State of Johor in Main method after sleep(): " + johor.getState());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Week_03/CheckStateAlive.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class CheckStateAlive {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | MyRunnable runnableJob = new MyRunnable();
8 | Thread thread = new Thread(runnableJob);
9 |
10 | displayStateAndIsAlive(thread);
11 | thread.start();
12 | displayStateAndIsAlive(thread);
13 | Thread.sleep(1000);
14 | displayStateAndIsAlive(thread);
15 | }
16 |
17 | public static void displayStateAndIsAlive(Thread thread) {
18 | System.out.println("State:" + thread.getState());
19 | System.out.println("Is alive?:" + thread.isAlive());
20 | }
21 |
22 | }
23 |
24 | class MyRunnable implements Runnable {
25 |
26 | @Override
27 | public void run() {
28 | System.out.println("MyRunnable is running");
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadAlive.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadAlive extends Thread {
4 |
5 | public void run() {
6 | for (int i = 0; i < 10; i++) {
7 | printMsg();
8 | }
9 | }
10 |
11 | public void printMsg() {
12 | Thread ct = Thread.currentThread();
13 | String name = ct.getName();
14 | System.out.println("name=" + name);
15 | }
16 |
17 | public static void main(String[] args) {
18 | ThreadAlive t = new ThreadAlive();
19 | System.out.println("before start(): " + t.getName() + " isAlive()= " + t.isAlive());
20 | t.start();
21 | System.out.println("after start(): " + t.getName() + " isAlive()= " + t.isAlive());
22 |
23 | for (int i = 0; i < 10; i++) {
24 | t.printMsg();
25 | }
26 | System.out.println("The end of main() method: " + t.getName() + " isAlive()= " + t.isAlive());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadBlocked.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class Shared {
4 |
5 | synchronized void methodOne(Shared s) {
6 | try {
7 | Thread.sleep(2000);
8 | } catch (InterruptedException e) {
9 | e.printStackTrace();
10 | }
11 | s.methodTwo(this);
12 | }
13 |
14 | synchronized void methodTwo(Shared s) {
15 | try {
16 | Thread.sleep(2000);
17 | } catch (InterruptedException e) {
18 | e.printStackTrace();
19 | }
20 | s.methodOne(this);
21 | }
22 | }
23 |
24 | class ThreadBlocked {
25 |
26 | public static void main(String[] args) {
27 |
28 | final Shared s1 = new Shared();
29 | final Shared s2 = new Shared();
30 |
31 | Thread t1 = new Thread() {
32 | @Override
33 | public void run() {
34 | s1.methodOne(s2);
35 | }
36 | };
37 |
38 | Thread t2 = new Thread() {
39 | @Override
40 | public void run() {
41 | s2.methodTwo(s1);
42 | }
43 | };
44 |
45 | t1.start();
46 | t2.start();
47 |
48 | try {
49 | Thread.sleep(3000);
50 | } catch (InterruptedException e) {
51 | e.printStackTrace();
52 | }
53 |
54 | System.out.println("state t1 = " + t1.getState());
55 | System.out.println("state t2 = " + t2.getState());
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadNew.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadNew {
4 |
5 | public static void main(String[] args) {
6 | Thread t = new Thread();
7 | System.out.println(t.getState());
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadRunnable.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadRunnable {
4 |
5 | public static void main(String[] args) {
6 | Thread t = new Thread();
7 | t.start();
8 | System.out.println(t.getState());
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadStates.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadStates {
4 |
5 | public static void main(String[] args) {
6 | Thread t = new Thread();
7 | Thread.State e = t.getState();
8 | Thread.State[] ts = e.values();
9 | for (int i = 0; i < ts.length; i++) {
10 | System.out.println(ts[i]);
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadStatesInJava.java:
--------------------------------------------------------------------------------
1 | //https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
2 |
3 | package Week_03;
4 |
5 | class ThreadStatesInJava {
6 |
7 | public static void main(String[] args) {
8 | Thread.State[] states = Thread.State.values();
9 |
10 | for (Thread.State state : states) {
11 | System.out.println(state);
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadTerminated.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadTerminated {
4 |
5 | public static void main(String[] args) {
6 | Thread t = new Thread() {
7 | @Override
8 | public void run() {
9 | for (int i = 0; i <= 10; i++) {
10 | System.out.println(i);
11 | }
12 | }
13 | };
14 |
15 | t.start();
16 |
17 | try {
18 | Thread.sleep(2000);
19 | } catch (InterruptedException e) {
20 | e.printStackTrace();
21 | }
22 | System.out.println(t.getState());
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadTimedWaiting.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadTimedWaiting {
4 |
5 | public static void main(String[] args) {
6 |
7 | Thread t = new Thread() {
8 | public void run() {
9 | try {
10 | Thread.sleep(5000);
11 | } catch (InterruptedException e) {
12 | e.printStackTrace();
13 | }
14 | }
15 | };
16 |
17 | t.start();
18 |
19 | try {
20 | Thread.sleep(2000);
21 | } catch (InterruptedException e) {
22 | e.printStackTrace();
23 | }
24 |
25 | System.out.println(t.getState());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Week_03/ThreadWaiting.java:
--------------------------------------------------------------------------------
1 | package Week_03;
2 |
3 | class ThreadWaiting {
4 |
5 | public static void main(String[] args) {
6 |
7 | Thread t1 = new Thread() {
8 | public void run() {
9 | try {
10 | Thread.sleep(2000);
11 | } catch (InterruptedException e) {
12 | e.printStackTrace();
13 | }
14 | }
15 | };
16 |
17 | Thread t2 = new Thread() {
18 | public void run() {
19 | try {
20 | t1.join();
21 | } catch (InterruptedException e) {
22 | e.printStackTrace();
23 | }
24 | }
25 | };
26 |
27 | t2.start();
28 | t1.start();
29 |
30 | try {
31 | Thread.sleep(100);
32 | } catch (InterruptedException e) {
33 | e.printStackTrace();
34 | }
35 |
36 | System.out.println(t2.getState());
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Week_04/Homework_WaitNotify.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class WaitNotifyThread extends Thread {
4 |
5 | boolean waiting = true;
6 | boolean ready = false;
7 |
8 | @Override
9 | public void run() {
10 | String tName = Thread.currentThread().getName();
11 | System.out.println(tName + " starting.");
12 |
13 | while (waiting) {
14 | System.out.println("waiting:" + waiting);
15 | }
16 | System.out.println("waiting...");
17 | startWait();
18 | try {
19 | Thread.sleep(1000);
20 | } catch (Exception exc) {
21 | System.out.println(tName + " interrupted.");
22 | }
23 | System.out.println(tName + " terminating.");
24 | }
25 |
26 | synchronized void startWait() {
27 | try {
28 | while (!ready) {
29 | wait();
30 | }
31 | } catch (InterruptedException exc) {
32 | System.out.println("wait() interrupted");
33 | }
34 | }
35 |
36 | synchronized void notice() {
37 | ready = true;
38 | notify();
39 | }
40 | }
41 |
42 | public class Homework_WaitNotify {
43 |
44 | public static void main(String args[]) throws Exception {
45 | WaitNotifyThread t1 = new WaitNotifyThread();
46 | t1.setName("MyThread #1");
47 | showThreadStatus(t1);
48 |
49 | t1.start();
50 | Thread.sleep(50);
51 | showThreadStatus(t1);
52 |
53 | t1.waiting = false;
54 | Thread.sleep(50);
55 | showThreadStatus(t1);
56 |
57 | t1.notice();
58 | Thread.sleep(50);
59 | showThreadStatus(t1);
60 |
61 | while (t1.isAlive()) {
62 | System.out.println("alive");
63 | }
64 | showThreadStatus(t1);
65 | }
66 |
67 | static void showThreadStatus(Thread thrd) {
68 | System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/Week_04/TestInterrupt1.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestInterrupt1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | System.out.println("#1:" + Thread.interrupted());
8 | Thread.currentThread().interrupt();
9 | System.out.println("#2:" + Thread.interrupted());
10 | System.out.println("#3:" + Thread.interrupted());
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Week_04/TestInterrupt2.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | public class TestInterrupt2 {
4 |
5 | public static void main(String[] args) {
6 | Thread t = new Thread(TestInterrupt2::run);
7 | t.start();
8 | try {
9 | Thread.currentThread().sleep(1000);
10 | } catch (InterruptedException e) {
11 | e.printStackTrace();
12 | }
13 | t.interrupt();
14 | }
15 |
16 | public static void run() {
17 | int counter = 0;
18 | while (!Thread.interrupted()) {
19 | counter++;
20 | }
21 | System.out.println("Counter:" + counter);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Week_04/TestInterrupt3.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | public class TestInterrupt3 {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 | Thread t = new Thread(TestInterrupt3::run);
7 | t.start();
8 | Thread.sleep(5000);
9 | t.interrupt();
10 | }
11 |
12 | public static void run() {
13 | int counter = 1;
14 | while (true) {
15 | try {
16 | Thread.sleep(1000);
17 | System.out.println("Counter:" + counter++);
18 | } catch (InterruptedException e) {
19 | System.out.println("I got interrupted!");
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Week_04/TestJoin1.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | public class TestJoin1 {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | Thread t1 = new Thread(new Runnable() {
8 | public void run() {
9 | System.out.println("First task started");
10 | System.out.println("Sleeping for 2 seconds");
11 | try {
12 | Thread.sleep(2000);
13 | } catch (InterruptedException e) {
14 | e.printStackTrace();
15 | }
16 |
17 | System.out.println("First task completed");
18 | }
19 | });
20 |
21 | Thread t2 = new Thread(new Runnable() {
22 | public void run() {
23 | System.out.println("Second task completed");
24 | }
25 | });
26 |
27 | t1.start();
28 | t1.join();
29 | t2.start();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Week_04/TestPriority1.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestPriority1 extends Thread {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("thread name is: " + Thread.currentThread().getName());
8 | System.out.println("thread priority is :" + Thread.currentThread().getPriority());
9 | }
10 |
11 | public static void main(String args[]) {
12 |
13 | TestPriority1 t1 = new TestPriority1();
14 | TestPriority1 t2 = new TestPriority1();
15 | t1.setPriority(Thread.MIN_PRIORITY);
16 | t2.setPriority(Thread.MAX_PRIORITY);
17 | t1.setName("satu");
18 | t2.setName("sepuluh");
19 | t1.start();
20 | t2.start();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Week_04/TestPriority2.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestPriority2 extends Thread {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("thread name is: " + Thread.currentThread().getName()
8 | + " --> Priority= " + Thread.currentThread().getPriority());
9 | }
10 |
11 | public static void main(String args[]) {
12 |
13 | TestPriority2 t1 = new TestPriority2();
14 | TestPriority2 t2 = new TestPriority2();
15 | System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getPriority());
16 |
17 | t1.setPriority(Thread.MIN_PRIORITY);
18 | t2.setPriority(Thread.MAX_PRIORITY);
19 | t1.setName("satu");
20 | t2.setName("sepuluh");
21 | t1.start();
22 | t2.start();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Week_04/TestPriority3.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestPriority3 extends Thread {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("Inside run method");
8 | }
9 |
10 | public static void main(String[] args) {
11 |
12 | Thread.currentThread().setPriority(8);
13 |
14 | System.out.println("main thread priority : "
15 | + Thread.currentThread().getPriority());
16 |
17 | TestPriority3 t1 = new TestPriority3();
18 |
19 | // t1 thread is child of main thread
20 | // so t1 thread will also have priority 8.
21 | System.out.println("t1 thread priority : " + t1.getPriority());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Week_04/TestSleep1.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestSleep1 {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | long start = System.currentTimeMillis();
8 | Thread.sleep(2000);
9 | System.out.println("Sleep time in ms = " + (System.currentTimeMillis() - start));
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Week_04/TestSleep2.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class TestSleep2 implements Runnable {
4 |
5 | @Override
6 | public void run() {
7 | for (int i = 1; i <= 5; i++) {
8 | System.out.println(Thread.currentThread().getName());
9 | try {
10 | // thread to sleep for 5000 milliseconds
11 | Thread.sleep(5000);
12 | } catch (Exception e) {
13 | System.out.println(e);
14 | }
15 | }
16 | }
17 |
18 | public static void main(String[] args) throws Exception {
19 |
20 | Thread t1 = new Thread(new TestSleep2(), "One");
21 | Thread t2 = new Thread(new TestSleep2(), "TWO");
22 | Thread t3 = new Thread(new TestSleep2(), "Three");
23 | t1.start();
24 | t2.start();
25 | t3.start();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Week_04/TestYield1.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | public class TestYield1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | Thread producer = new Producer();
8 | Thread consumer = new Consumer();
9 |
10 | producer.setPriority(Thread.MIN_PRIORITY); //Min Priority
11 | consumer.setPriority(Thread.MAX_PRIORITY); //Max Priority
12 |
13 | producer.start();
14 | consumer.start();
15 | }
16 | }
17 |
18 | class Producer extends Thread {
19 |
20 | public void run() {
21 | for (int i = 1; i <= 5; i++) {
22 | System.out.println("Producer : Produced Item " + i);
23 | Thread.yield();
24 | }
25 | }
26 | }
27 |
28 | class Consumer extends Thread {
29 |
30 | public void run() {
31 | for (int i = 1; i <= 5; i++) {
32 | System.out.println("Consumer : Consumed Item " + i);
33 | Thread.yield();
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Week_04/TestYield2.java:
--------------------------------------------------------------------------------
1 | package Week_04;
2 |
3 | class YThread extends Thread {
4 |
5 | private String name;
6 |
7 | public YThread(String name) {
8 | this.name = name;
9 | }
10 |
11 | @Override
12 | public void run() {
13 | for (int i = 1; i <= 5; ++i) {
14 | System.out.println(name + ": " + i);
15 | yield();
16 | }
17 | }
18 | }
19 |
20 | class TestYield2 {
21 |
22 | public static void main(String[] args) {
23 | Thread[] threads = {
24 | new YThread("One"),
25 | new YThread("TWO"),
26 | new YThread("Three")
27 | };
28 | for (Thread t : threads) {
29 | t.start();
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Week_05/Images/Atomic-Classes.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhamri/Java-Concurrency/7e1078f38960741df5e5072c3c80d75a88bf2274/src/Week_05/Images/Atomic-Classes.png
--------------------------------------------------------------------------------
/src/Week_05/Images/Atomic-Operation.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhamri/Java-Concurrency/7e1078f38960741df5e5072c3c80d75a88bf2274/src/Week_05/Images/Atomic-Operation.png
--------------------------------------------------------------------------------
/src/Week_05/Images/Common-Atomic-Methods.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhamri/Java-Concurrency/7e1078f38960741df5e5072c3c80d75a88bf2274/src/Week_05/Images/Common-Atomic-Methods.png
--------------------------------------------------------------------------------
/src/Week_05/Images/Thread-Unsafe.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhamri/Java-Concurrency/7e1078f38960741df5e5072c3c80d75a88bf2274/src/Week_05/Images/Thread-Unsafe.png
--------------------------------------------------------------------------------
/src/Week_05/MyExecuter.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_05;
3 |
4 | import java.util.concurrent.ExecutorService;
5 | import java.util.concurrent.Executors;
6 |
7 | class ExecuterRunnable implements Runnable {
8 |
9 | private final long countUntil;
10 |
11 | ExecuterRunnable(long countUntil) {
12 | this.countUntil = countUntil;
13 | }
14 |
15 | @Override
16 | public void run() {
17 | long sum = 0;
18 | for (long i = 1; i < countUntil; i++) {
19 | sum += i;
20 | }
21 | System.out.println(sum);
22 | }
23 | }
24 |
25 | public class MyExecuter {
26 |
27 | private static final int MYPOOL = 10;
28 |
29 | public static void main(String[] args) {
30 | ExecutorService executor = Executors.newFixedThreadPool(MYPOOL);
31 | for (int i = 0; i < 20; i++) {
32 | Runnable worker = new ExecuterRunnable(10000000L + i);
33 | executor.execute(worker);
34 | }
35 | executor.shutdown();
36 | while (!executor.isTerminated()) {
37 | }
38 | System.out.println("Finished all threads");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/Week_05/SheepManager1p.java:
--------------------------------------------------------------------------------
1 | package Week_05;
2 |
3 | import java.util.concurrent.ExecutorService;
4 | import java.util.concurrent.Executors;
5 |
6 | public class SheepManager1p {
7 |
8 | private int sheepCount = 0;
9 |
10 | private void incrementAndReport() {
11 | System.out.print((++sheepCount) + " ");
12 | }
13 |
14 | public static void main(String[] args) {
15 | ExecutorService service = null;
16 | try {
17 | service = Executors.newFixedThreadPool(20);
18 | SheepManager1p manager = new SheepManager1p();
19 | for (int i = 0; i < 10; i++) {
20 | service.submit(() -> manager.incrementAndReport());
21 | }
22 | } finally {
23 | if (service != null) {
24 | service.shutdown();
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Week_05/SheepManager1s1.java:
--------------------------------------------------------------------------------
1 | package Week_05;
2 |
3 | import java.util.concurrent.ExecutorService;
4 | import java.util.concurrent.Executors;
5 | import java.util.concurrent.atomic.AtomicInteger; // New
6 |
7 | public class SheepManager1s1 {
8 |
9 | // private int sheepCount = 0;
10 | private AtomicInteger sheepCount = new AtomicInteger(0);
11 |
12 | private void incrementAndReport() {
13 | // System.out.print((++sheepCount) + " ");
14 | System.out.print(sheepCount.incrementAndGet() + " ");
15 | }
16 |
17 | public static void main(String[] args) {
18 | ExecutorService service = null;
19 | try {
20 | service = Executors.newFixedThreadPool(20);
21 | SheepManager1s1 manager = new SheepManager1s1();
22 | for (int i = 0; i < 10; i++) {
23 | service.submit(() -> manager.incrementAndReport());
24 | }
25 | } finally {
26 | if (service != null) {
27 | service.shutdown();
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Week_05/SheepManager1s2.java:
--------------------------------------------------------------------------------
1 | package Week_05;
2 |
3 | import java.util.concurrent.ExecutorService;
4 | import java.util.concurrent.Executors;
5 | import java.util.concurrent.atomic.AtomicInteger; // New
6 |
7 | public class SheepManager1s2 {
8 |
9 | // private int sheepCount = 0;
10 | private AtomicInteger sheepCount = new AtomicInteger(0);
11 |
12 | private void incrementAndReport() {
13 | synchronized(this){
14 | // System.out.print((++sheepCount) + " ");
15 | System.out.print(sheepCount.incrementAndGet() + " ");
16 | }
17 | }
18 |
19 | public static void main(String[] args) {
20 | ExecutorService service = null;
21 | try {
22 | service = Executors.newFixedThreadPool(20);
23 | SheepManager1s2 manager = new SheepManager1s2();
24 | for (int i = 0; i < 10; i++) {
25 | service.submit(() -> manager.incrementAndReport());
26 | }
27 | } finally {
28 | if (service != null) {
29 | service.shutdown();
30 | }
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Week_05/TestAtomicInteger1p.java:
--------------------------------------------------------------------------------
1 | package Week_05;
2 |
3 | class TestAtomicInteger1p {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | CountProblem pt = new CountProblem();
8 | Thread t1 = new Thread(pt, "t1");
9 | Thread t2 = new Thread(pt, "t2");
10 | t1.start();
11 | t2.start();
12 | t1.join();
13 | t2.join();
14 | System.out.println("Count=" + pt.getCount());
15 | }
16 | }
17 |
18 | class CountProblem implements Runnable {
19 |
20 | private int count;
21 |
22 | @Override
23 | public void run() {
24 | for (int i = 1; i <= 5; i++) {
25 | processSomething(i);
26 | count++;
27 | }
28 | }
29 |
30 | public int getCount() {
31 | return this.count;
32 | }
33 |
34 | private void processSomething(int i) {
35 | try {
36 | Thread.sleep(i * 200);
37 | } catch (InterruptedException e) {
38 | e.printStackTrace();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Week_05/TestAtomicInteger1s.java:
--------------------------------------------------------------------------------
1 | package Week_05;;
2 |
3 | import java.util.concurrent.atomic.AtomicInteger;
4 |
5 | class TestAtomicInteger1s {
6 |
7 | public static void main(String[] args) throws InterruptedException {
8 |
9 | CountSolution pt = new CountSolution();
10 | Thread t1 = new Thread(pt, "t1");
11 | Thread t2 = new Thread(pt, "t2");
12 | t1.start();
13 | t2.start();
14 | t1.join();
15 | t2.join();
16 | System.out.println("Count=" + pt.getCount());
17 | }
18 |
19 | }
20 |
21 | class CountSolution implements Runnable {
22 |
23 | // private int count;
24 | private AtomicInteger count = new AtomicInteger(); //New
25 |
26 | @Override
27 | public void run() {
28 | for (int i = 1; i <= 5; i++) {
29 | processSomething(i);
30 | // count++;
31 | count.incrementAndGet(); //New
32 | }
33 | }
34 |
35 | public int getCount() {
36 | // return this.count;
37 | return this.count.get(); //New
38 | }
39 |
40 | private void processSomething(int i) {
41 | try {
42 | Thread.sleep(i * 200);
43 | } catch (InterruptedException e) {
44 | e.printStackTrace();
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/Week_06/TestSynchronized1.java:
--------------------------------------------------------------------------------
1 | package Week_06;
2 |
3 | class TestSynchronized1 extends Thread {
4 |
5 | public static void main(String[] args) {
6 |
7 | TestSynchronized1 ts = new TestSynchronized1();
8 | Thread boy = new Thread(ts);
9 | Thread girl = new Thread(ts);
10 | boy.setName("boy");
11 | girl.setName("girl");
12 | boy.start();
13 | girl.start();
14 | }
15 |
16 | public void run() {
17 | synchronized (this) {
18 | for (int i = 0; i <= 10; i++) {
19 | System.out.println(Thread.currentThread().getName() + "..." + i);
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Week_06/TestSynchronized2p.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_06;
3 |
4 | public class TestSynchronized2p {
5 |
6 | private static int count;
7 |
8 | public static void MyThread() {
9 |
10 | Thread t1 = new Thread() {
11 | public void run() {
12 | for (int i = 0; i < 1000; i++) {
13 | count++;
14 | }
15 | }
16 | };
17 | Thread t2 = new Thread() {
18 | public void run() {
19 | for (int i = 0; i < 1000; i++) {
20 | count++;
21 | }
22 | }
23 | };
24 | t1.start();
25 | t2.start();
26 | }
27 |
28 | public static void main(String[] args) {
29 | MyThread();
30 | System.out.println("count= " + count);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Week_06/TestSynchronized2s1.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_06;
3 |
4 | public class TestSynchronized2s1 {
5 |
6 | private static int count;
7 |
8 | public static synchronized void count(){
9 | count++;
10 | }
11 |
12 | public static void MyThread() throws InterruptedException {
13 |
14 | Thread t1 = new Thread() {
15 | public void run() {
16 | for (int i = 0; i < 1000; i++) {
17 | count();
18 | }
19 | }
20 | };
21 | Thread t2 = new Thread() {
22 | public void run() {
23 | for (int i = 0; i < 1000; i++) {
24 | count();
25 | }
26 | }
27 | };
28 | t1.start();
29 | t2.start();
30 | t1.join();
31 | t2.join();
32 | }
33 |
34 | public static void main(String[] args) throws InterruptedException {
35 | MyThread();
36 | System.out.println("count= " + count);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Week_06/TestSynchronizedCounter.java:
--------------------------------------------------------------------------------
1 |
2 | package Week_06;
3 |
4 | class SynchronizedCounter {
5 |
6 | private static int count = 0;
7 |
8 | public synchronized static void increment() {
9 | ++count;
10 | System.out.println("Count is " + count + " @ " + System.nanoTime());
11 | }
12 |
13 | public synchronized static void decrement() {
14 | --count;
15 | System.out.println("Count is " + count + " @ " + System.nanoTime());
16 | }
17 | }
18 |
19 | class TestSynchronizedCounter {
20 |
21 | public static void main(String[] args) {
22 | Thread threadIncrement = new Thread() {
23 | @Override
24 | public void run() {
25 | for (int i = 0; i < 10; ++i) {
26 | SynchronizedCounter.increment();
27 | try {
28 | sleep(1);
29 | } catch (InterruptedException e) {
30 | }
31 | }
32 | }
33 | };
34 |
35 | Thread threadDecrement = new Thread() {
36 | @Override
37 | public void run() {
38 | for (int i = 0; i < 10; ++i) {
39 | SynchronizedCounter.decrement();
40 | try {
41 | sleep(1);
42 | } catch (InterruptedException e) {
43 | }
44 | }
45 | }
46 | };
47 |
48 | threadIncrement.start();
49 | threadDecrement.start();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Week_07/PleaseRead.txt:
--------------------------------------------------------------------------------
1 | Please Read
2 |
3 | https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html
4 |
5 | https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html
6 |
7 | https://netjs.blogspot.my/2016/02/difference-between-reentrantlock-and-synchronized-java.html
8 |
9 | https://netjs.blogspot.my/2016/02/reentrantlock-in-java-concurrency.html
10 |
--------------------------------------------------------------------------------
/src/Week_07/StudentLibrary/Book.java:
--------------------------------------------------------------------------------
1 | package Week_07.StudentLibrary;
2 |
3 | import java.util.concurrent.TimeUnit;
4 | import java.util.concurrent.locks.Lock;
5 | import java.util.concurrent.locks.ReentrantLock;
6 |
7 | public class Book {
8 |
9 | private int id;
10 | private Lock lock;
11 |
12 | public Book(int id) {
13 | this.lock = new ReentrantLock();
14 | this.id = id;
15 | }
16 |
17 | public void read(Student student) throws InterruptedException {
18 | lock.tryLock(10, TimeUnit.MINUTES);
19 | System.out.println(student + " starts reading " + this);
20 | Thread.sleep(2000);
21 | lock.unlock();
22 | System.out.println(student + " has finished reading " + this);
23 | }
24 |
25 | public String toString() {
26 | return "Book-" + id;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/Week_07/StudentLibrary/Constants.java:
--------------------------------------------------------------------------------
1 | package Week_07.StudentLibrary;
2 |
3 | public class Constants {
4 |
5 | public static final int NUMBER_OF_STUDENTS = 5;
6 | public static final int NUMBER_OF_BOOKS = 7;
7 | }
8 |
--------------------------------------------------------------------------------
/src/Week_07/StudentLibrary/Student.java:
--------------------------------------------------------------------------------
1 | package Week_07.StudentLibrary;
2 |
3 | import java.util.Random;
4 |
5 | public class Student implements Runnable {
6 |
7 | private int id;
8 | private Book[] books;
9 |
10 | public Student(int id, Book[] books) {
11 | this.books = books;
12 | this.id = id;
13 | }
14 |
15 | @Override
16 | public void run() {
17 |
18 | Random random = new Random();
19 |
20 | while (true) {
21 |
22 | int bookId = random.nextInt(Constants.NUMBER_OF_BOOKS);
23 |
24 | try {
25 | books[bookId].read(this);
26 | } catch (InterruptedException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | }
31 |
32 | public String toString() {
33 | return "Student-" + id;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Week_07/StudentLibrary/TestApp.java:
--------------------------------------------------------------------------------
1 | package Week_07.StudentLibrary;
2 |
3 | import java.util.concurrent.ExecutorService;
4 | import java.util.concurrent.Executors;
5 |
6 | public class TestApp {
7 |
8 | public static void main(String[] args) {
9 |
10 | Student[] students = null;
11 | Book[] books = null;
12 | ExecutorService executor = Executors.newFixedThreadPool(Constants.NUMBER_OF_STUDENTS);
13 |
14 | try {
15 |
16 | books = new Book[Constants.NUMBER_OF_BOOKS];
17 | students = new Student[Constants.NUMBER_OF_STUDENTS];
18 |
19 | for (int i = 0; i < Constants.NUMBER_OF_BOOKS; i++) {
20 | books[i] = new Book(i);
21 | }
22 |
23 | for (int i = 0; i < Constants.NUMBER_OF_STUDENTS; i++) {
24 | students[i] = new Student(i, books);
25 | executor.execute(students[i]);
26 | }
27 | } catch (Exception e) {
28 | e.printStackTrace();
29 | executor.shutdown();
30 | } finally {
31 | executor.shutdown();
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Week_07/TestLock1.java:
--------------------------------------------------------------------------------
1 | //https://howtodoinjava.com/core-java/multi-threading/how-to-use-locks-in-java-java-util-concurrent-locks-lock-tutorial-and-example/
2 |
3 | package Week_07;
4 |
5 | import java.util.Date;
6 | import java.util.concurrent.locks.Lock;
7 | import java.util.concurrent.locks.ReentrantLock;
8 |
9 | class PrintingJob implements Runnable {
10 |
11 | private PrinterQueue printerQueue;
12 |
13 | public PrintingJob(PrinterQueue printerQueue) {
14 | this.printerQueue = printerQueue;
15 | }
16 |
17 | @Override
18 | public void run() {
19 | System.out.printf("%s: Going to print a document\n", Thread.currentThread().getName());
20 | printerQueue.printJob(new Object());
21 | }
22 | }
23 |
24 | class PrinterQueue {
25 |
26 | private final Lock queueLock = new ReentrantLock();
27 |
28 | public void printJob(Object document) {
29 | queueLock.lock();
30 | try {
31 | Long duration = (long) (Math.random() * 10000);
32 | System.out.println(Thread.currentThread().getName() + ": PrintQueue: Printing a Job during " + (duration / 1000) + " seconds :: Time - " + new Date());
33 | Thread.sleep(duration);
34 | } catch (InterruptedException e) {
35 | e.printStackTrace();
36 | } finally {
37 | System.out.printf("%s: The document has been printed\n", Thread.currentThread().getName());
38 | queueLock.unlock();
39 | }
40 | }
41 | }
42 |
43 | public class TestLock1 {
44 |
45 | public static void main(String[] args) {
46 | PrinterQueue printerQueue = new PrinterQueue();
47 | Thread thread[] = new Thread[10];
48 | for (int i = 0; i < 10; i++) {
49 | thread[i] = new Thread(new PrintingJob(printerQueue), "Thread " + i);
50 | }
51 | for (int i = 0; i < 10; i++) {
52 | thread[i].start();
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Week_07/TestLock1p.java:
--------------------------------------------------------------------------------
1 | package Week_07;
2 |
3 | public class TestLock1p {
4 |
5 | private static int counter = 0;
6 |
7 | public static void increment() {
8 | counter++;
9 | }
10 |
11 | public static void firstThread() {
12 | for (int i = 0; i < 1000; i++) {
13 | increment();
14 | }
15 | }
16 |
17 | public static void secondThread() {
18 | for (int i = 0; i < 1000; i++) {
19 | increment();
20 | }
21 | }
22 |
23 | public static void main(String[] args) {
24 |
25 | Thread t1 = new Thread(new Runnable() {
26 | public void run() {
27 | firstThread();
28 | }
29 | });
30 |
31 | Thread t2 = new Thread(new Runnable() {
32 | public void run() {
33 | firstThread();
34 | }
35 | });
36 |
37 | t1.start();
38 | t2.start();
39 |
40 | try {
41 | t1.join();
42 | t2.join();
43 | } catch (InterruptedException e) {
44 | e.printStackTrace();
45 | }
46 | System.out.println(counter);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Week_07/TestLock1s.java:
--------------------------------------------------------------------------------
1 | package Week_07;
2 |
3 | import java.util.concurrent.locks.Lock;
4 | import java.util.concurrent.locks.ReentrantLock;
5 |
6 | public class TestLock1s {
7 |
8 | private static int counter = 0;
9 | private static Lock lock = new ReentrantLock();
10 |
11 | public static void increment() {
12 | lock.lock();
13 | counter++;
14 | lock.unlock();
15 | }
16 |
17 | public static void firstThread() {
18 | for (int i = 0; i < 1000; i++) {
19 | increment();
20 | }
21 | }
22 |
23 | public static void secondThread() {
24 | for (int i = 0; i < 1000; i++) {
25 | increment();
26 | }
27 | }
28 |
29 | public static void main(String[] args) {
30 |
31 | Thread t1 = new Thread(new Runnable() {
32 | public void run() {
33 | firstThread();
34 | }
35 | });
36 |
37 | Thread t2 = new Thread(new Runnable() {
38 | public void run() {
39 | firstThread();
40 | }
41 | });
42 |
43 | t1.start();
44 | t2.start();
45 |
46 | try {
47 | t1.join();
48 | t2.join();
49 | } catch (InterruptedException e) {
50 | e.printStackTrace();
51 | }
52 | System.out.println(counter);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Week_08/TestStopThread1.java:
--------------------------------------------------------------------------------
1 | //http://javaconceptoftheday.com/how-to-stop-a-thread-in-java/
2 | package Week_08;
3 |
4 | class MyThread extends Thread {
5 |
6 | @Override
7 | public void run() {
8 | int count = 0;
9 | while (!Thread.interrupted()) {
10 | System.out.println("I am running ..." + count++);
11 | }
12 | System.out.println("Stopped Running ....");
13 | }
14 | }
15 |
16 | public class TestStopThread1 {
17 |
18 | public static void main(String[] args) {
19 |
20 | MyThread thread = new MyThread();
21 | thread.start();
22 |
23 | try {
24 | Thread.sleep(100);
25 | } catch (InterruptedException e) {
26 | e.printStackTrace();
27 | }
28 | //interrupting the thread
29 | thread.interrupt();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Week_08/TestStopThread2.java:
--------------------------------------------------------------------------------
1 | package Week_08;
2 |
3 | public class TestStopThread2 {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | ThreadStop stop = new ThreadStop();
8 | Thread t1 = new Thread(stop);
9 | t1.start();
10 | Thread.sleep(3000);
11 | t1.interrupt();
12 |
13 | try {
14 | Thread.sleep(3000);
15 | } catch (InterruptedException e) {
16 | Thread.currentThread().interrupt();
17 | }
18 |
19 | System.out.println("Finished...");
20 | }
21 | }
22 |
23 | class ThreadStop implements Runnable {
24 |
25 | @Override
26 | public void run() {
27 | int count = 0;
28 | while (!Thread.interrupted()) {
29 | System.out.println("Hello from STIW3054 class... " + count++);
30 | try {
31 | Thread.sleep(300);
32 | } catch (InterruptedException e) {
33 | Thread.currentThread().interrupt();
34 | }
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/Week_08/TestTerminated1.java:
--------------------------------------------------------------------------------
1 | package Week_08;
2 |
3 | class Worker implements Runnable {
4 |
5 | private boolean isTerminated = false;
6 |
7 | @Override
8 | public void run() {
9 | int count = 0;
10 | while (!isTerminated) {
11 |
12 | System.out.println("Hello from worker class... " + count++ );
13 |
14 | try {
15 | Thread.sleep(300);
16 | } catch (InterruptedException e) {
17 | e.printStackTrace();
18 | }
19 | }
20 | }
21 |
22 | public boolean isTerminated() {
23 | return isTerminated;
24 | }
25 |
26 | public void setTerminated(boolean isTerminated) {
27 | this.isTerminated = isTerminated;
28 | }
29 | }
30 |
31 | public class TestTerminated1 {
32 |
33 | public static void main(String[] args) {
34 |
35 | Worker worker = new Worker();
36 | Thread t1 = new Thread(worker);
37 | t1.start();
38 |
39 | try {
40 | Thread.sleep(3000);
41 | } catch (InterruptedException e) {
42 | e.printStackTrace();
43 | }
44 |
45 | worker.setTerminated(true);
46 | System.out.println("Finished...");
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Week_08/TestVolatile1.java:
--------------------------------------------------------------------------------
1 | package Week_08;
2 |
3 | import java.util.Scanner;
4 |
5 | class MyRunning extends Thread {
6 |
7 | private volatile boolean running = true;
8 |
9 | public void run() {
10 | while (running) {
11 | System.out.println("Running");
12 |
13 | try {
14 | Thread.sleep(100);
15 | } catch (InterruptedException e) {
16 | e.printStackTrace();
17 | }
18 | }
19 | }
20 |
21 | public void shutdown() {
22 | running = false;
23 | }
24 | }
25 |
26 | public class TestVolatile1 {
27 |
28 | public static void main(String[] args) {
29 | MyRunning pro = new MyRunning();
30 | pro.start();
31 | System.out.println("Enter something to stop the thread: ");
32 | new Scanner(System.in).nextLine();
33 | pro.shutdown();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Week_09/test.txt:
--------------------------------------------------------------------------------
1 | This is a test.
2 |
--------------------------------------------------------------------------------
/src/Week_13/ParallelSum.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | public class ParallelSum {
4 |
5 | private ParallelWorker[] sums;
6 | private int numOfThreads;
7 |
8 | public ParallelSum(int numOfThreads) {
9 | this.sums = new ParallelWorker[numOfThreads];
10 | this.numOfThreads = numOfThreads;
11 | }
12 |
13 | public int parallelSum(int[] nums) {
14 |
15 | int size = (int) Math.ceil(nums.length * 1.0 / numOfThreads);
16 |
17 | for (int i = 0; i < numOfThreads; i++) {
18 | sums[i] = new ParallelWorker(nums, i * size, (i + 1) * size);
19 | sums[i].start();
20 | }
21 |
22 | try {
23 | for (ParallelWorker sum : sums) {
24 | sum.join();
25 | }
26 | } catch (InterruptedException e) {
27 | e.printStackTrace();
28 | }
29 |
30 | int total = 0;
31 |
32 | for (ParallelWorker sum : sums) {
33 | total += sum.getPartialSum();
34 | }
35 |
36 | return total;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/Week_13/ParallelWorker.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | public class ParallelWorker extends Thread {
4 |
5 | private int[] nums;
6 | private int low;
7 | private int high;
8 | private int partialSum;
9 |
10 | public ParallelWorker(int[] nums, int low, int high) {
11 | this.nums = nums;
12 | this.low = low;
13 | this.high = Math.min(high, nums.length);
14 | }
15 |
16 | public int getPartialSum() {
17 | return partialSum;
18 | }
19 |
20 | @Override
21 | public void run() {
22 |
23 | partialSum = 0;
24 |
25 | for (int i = low; i < high; i++) {
26 | partialSum += nums[i];
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Week_13/SequentialSum.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | public class SequentialSum {
4 |
5 | public int sum(int[] nums) {
6 |
7 | int total = 0;
8 |
9 | for (int i = 0; i < nums.length; ++i) {
10 | total += nums[i];
11 | }
12 |
13 | return total;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Week_13/TestParallel.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | import java.util.Random;
4 |
5 | public class TestParallel {
6 |
7 | public static void main(String[] args) {
8 |
9 | Random random = new Random();
10 | SequentialSum sequentialSum = new SequentialSum();
11 |
12 | int numOfProcessors = Runtime.getRuntime().availableProcessors();
13 |
14 | int[] nums = new int[100000000];
15 |
16 | for (int i = 0; i < nums.length; i++) {
17 | nums[i] = random.nextInt(101) + 1;
18 | System.out.println(nums[i]);
19 | }
20 |
21 | long start = System.currentTimeMillis();
22 | System.out.println("Sum is: " + sequentialSum.sum(nums));
23 | System.out.println("Single: " + (System.currentTimeMillis() - start) + "ms");
24 |
25 | start = System.currentTimeMillis();
26 | ParallelSum parallelSum = new ParallelSum(numOfProcessors);
27 | System.out.println("Sum is: " + parallelSum.parallelSum(nums));
28 | System.out.println("Parallel: " + (System.currentTimeMillis() - start) + "ms");
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Week_13/TestRecursiveAction1.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | import java.util.concurrent.ForkJoinPool;
4 | import java.util.concurrent.RecursiveAction;
5 |
6 | class SimpleRecursiveAction extends RecursiveAction {
7 |
8 | private int simulateWork;
9 |
10 | public SimpleRecursiveAction(int simulateWork) {
11 | this.simulateWork = simulateWork;
12 | }
13 |
14 | @Override
15 | protected void compute() {
16 | if (simulateWork > 100) {
17 | System.out.println("Parallel execution ... " + simulateWork);
18 | SimpleRecursiveAction action1 = new SimpleRecursiveAction(simulateWork / 2);
19 | SimpleRecursiveAction action2 = new SimpleRecursiveAction(simulateWork / 2);
20 | action1.fork();
21 | action2.fork();
22 | } else {
23 | System.out.println("Sequential execution ..." + simulateWork);
24 | }
25 | }
26 | }
27 |
28 | public class TestRecursiveAction1 {
29 |
30 | public static void main(String[] args) {
31 | ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
32 | SimpleRecursiveAction action = new SimpleRecursiveAction(120);
33 | pool.invoke(action);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Week_13/TestRecursiveTask1.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | import java.util.concurrent.ForkJoinPool;
4 | import java.util.concurrent.RecursiveTask;
5 |
6 | class SimpleRecursiveTask extends RecursiveTask {
7 |
8 | private int simulateWork;
9 |
10 | public SimpleRecursiveTask(int simulateWork) {
11 | this.simulateWork = simulateWork;
12 | }
13 |
14 | @Override
15 | protected Integer compute() {
16 | if (simulateWork > 100) {
17 | System.out.println("Parallel execution ... " + simulateWork);
18 | SimpleRecursiveTask task1 = new SimpleRecursiveTask(simulateWork / 2);
19 | SimpleRecursiveTask task2 = new SimpleRecursiveTask(simulateWork / 2);
20 | task1.fork();
21 | task2.fork();
22 |
23 | int solution = 0;
24 | solution += task1.join();
25 | solution += task1.join();
26 |
27 | return solution;
28 |
29 | } else {
30 | System.out.println("Sequential execution ..." + simulateWork);
31 | return 2 * simulateWork;
32 | }
33 | }
34 | }
35 |
36 | public class TestRecursiveTask1 {
37 |
38 | public static void main(String[] args) {
39 | ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
40 | SimpleRecursiveTask task = new SimpleRecursiveTask(120);
41 | System.out.println(pool.invoke(task));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/Week_13/TestSequential.java:
--------------------------------------------------------------------------------
1 | package Week_13;
2 |
3 | import java.util.Random;
4 |
5 | public class TestSequential {
6 |
7 | public static void main(String[] args) {
8 |
9 | Random random = new Random();
10 | SequentialSum sequentialSum = new SequentialSum();
11 |
12 | int[] nums = new int[100000000];
13 |
14 | for (int i = 0; i < nums.length; i++) {
15 | nums[i] = random.nextInt(101) + 1;
16 | System.out.println(nums[i]);
17 | }
18 |
19 | long start = System.currentTimeMillis();
20 | System.out.println("Sum is: " + sequentialSum.sum(nums));
21 | System.out.println("Single: " + (System.currentTimeMillis() - start) + "ms");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------