|
|
|
oracle tutorials многопоточность
|
|||
|---|---|---|---|
|
#18+
Решил восполнить пробел и немного подтянуть скилл многопоточности в джаве. Вот например: Код: java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. Это из туториалов оракла, и вопрос: всегда ли будет выводиться Mares do eat oats и если нет, то можно ли так сделать. И вот ответ: авторThere are two ways you can guarantee that all changes to message will be visible to the main thread: 1.In the main thread, retain a reference to the CorrectorThread instance. Then invoke join on that instance before referring to message 2.Encapsulate message in an object with synchronized methods. Never reference message except through those methods. Both of these techniques establish the necessary happens-before relationship, making changes to message visible. 3.A third technique is to simply declare message as volatile. This guarantees that any write to message (as in "Key statement 1") will have a happens-before relationship with any subsequent reads of message (as in "Key statement 2"). But it does not guarantee that "Key statement 1" will literally happen before "Key statement 2" . They will probably happen in sequence, but because of scheduling uncertainities and the unknown granularity of sleep, this is not guaranteed. А что, разве второй вариант с синхронизацией как-то поможет сделать, что "Key statement 1" will literally happen before "Key statement 2"? Мне всегда казалось, что вариант здесь только один - с join (первый), второй и третий - одинаковые в данном случае. Тоесть здесь как-бы дата рэйс и мемори инконсистенси, второе решается всеми тремя вариантами, а первое - только первым. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 12.10.2015, 16:45 |
|
||
|
oracle tutorials многопоточность
|
|||
|---|---|---|---|
|
#18+
Ну вообще соглашусь с вами, вы ничего не вырезали из середины текста и какая точная формулировка вопроса? ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 12.10.2015, 18:22 |
|
||
|
oracle tutorials многопоточность
|
|||
|---|---|---|---|
|
#18+
Обсуждение этого вопроса более наглядно если рисовать временную диаграмму работы потоков. Тоесть где поток создан. Где работает. Где завершил работу. Там же и рассмотреть разные кейсы ситуаций. Что ДО что ПОСЛЕ. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 12.10.2015, 18:55 |
|
||
|
|

start [/forum/topic.php?fid=59&fpage=116&tid=2124832]: |
0ms |
get settings: |
7ms |
get forum list: |
14ms |
check forum access: |
3ms |
check topic access: |
3ms |
track hit: |
47ms |
get topic data: |
8ms |
get forum data: |
2ms |
get page messages: |
34ms |
get tp. blocked users: |
1ms |
| others: | 203ms |
| total: | 322ms |

| 0 / 0 |
