Advertisement
Air_neko

Skype talk about Threads

May 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. [10/05/2017 10:07:26] Simon Asaert: Hey jp. Can I steal you one of these days to teach me how to use threads properly?
  2. [10/05/2017 10:11:45] Simon Asaert: For my project thingy I'm doing in my uni require them but they were mentioned as a footnote of a PowerPoint slide
  3. [10/05/2017 11:16:00] Simon Asaert: Plus I'm sure it is needed knowledge for working with you on whatever you need me to do xD
  4. [10/05/2017 12:25:39] Jaap | jpresent: Hey :)
  5. [10/05/2017 12:25:46] Simon Asaert: o/
  6. [10/05/2017 12:25:48] Jaap | jpresent: Threads are fairly easy to use
  7. [10/05/2017 12:25:53] Jaap | jpresent: What do you want to know about them?
  8. [10/05/2017 12:26:07] Simon Asaert: Just... How they work really, how to call them etc.
  9. [10/05/2017 12:26:36] Jaap | jpresent: new Thread(() -> {
  10. //Do stuff async
  11. }).start();
  12. [10/05/2017 12:26:41] Jaap | jpresent: x)
  13. [10/05/2017 12:26:43] Jaap | jpresent: Easy as crap
  14. [10/05/2017 12:26:50] Jaap | jpresent: Do you know lambda expressions?
  15. [10/05/2017 12:27:15] Simon Asaert: Not from the top of my head x.x
  16. [10/05/2017 12:27:47] Jaap | jpresent: () -> {} is the same as new Runnable() {
  17. @Override
  18. public void run() {
  19. }
  20. }
  21. [10/05/2017 12:27:58] Simon Asaert: Oh right yeah
  22. [10/05/2017 12:29:55] Jaap | jpresent: Ok, so, the basics
  23. [10/05/2017 12:30:05] Jaap | jpresent: A thread is an object with a runnable in it
  24. [10/05/2017 12:30:18] Jaap | jpresent: To start an async thread, use thread.start()
  25. [10/05/2017 12:30:25] Jaap | jpresent: To start a sync thread, use thread.run()
  26. [10/05/2017 12:30:56] Jaap | jpresent: To see if a thread has died (might take longer than the actual excecution) use thread.join() (might never happen if the thread blocks)
  27. [10/05/2017 12:31:24] Jaap | jpresent: You can synchronize objects with other threads, so they don't use the object at the same time
  28. [10/05/2017 12:31:35] Jaap | jpresent: But I prefer to use concurrent versions of the objects
  29. [10/05/2017 12:31:41] Simon Asaert: logical
  30. [10/05/2017 12:31:59] Jaap | jpresent: Do you know about the concurrent object and atomic objects?
  31. [10/05/2017 12:32:09] Simon Asaert: Not atomic objects
  32. [10/05/2017 12:32:47] Jaap | jpresent: Atomic is basically the primary objects like integer and boolean, but then they can safely be accessed by multiple threads
  33. [10/05/2017 12:33:15] Jaap | jpresent: There is a one in a billion chance that two threads modify an object at the same time, which could cause unexpected results
  34. [10/05/2017 12:33:36] Jaap | jpresent: Just like concurrent objects, atomic objects pause all other threads until the one that is currently working on the object is finished with it
  35. [10/05/2017 12:33:38] Jaap | jpresent: It's lovely
  36. [10/05/2017 12:33:52] Simon Asaert: That sounds super helpful
  37. [10/05/2017 12:34:20] Simon Asaert: So if two threads at the same time call for the object to modify, it pauses one of them, allows the other to modify, then allows the paused one to unpause and modify. Very helpful
  38. [10/05/2017 12:34:20] Jaap | jpresent: Basically
  39. [10/05/2017 12:34:28] Jaap | jpresent: Yes
  40. [10/05/2017 12:34:42] Jaap | jpresent: If you ever see a ConcurrentModificationException
  41. [10/05/2017 12:34:54] Jaap | jpresent: Just use one of the atomic or concurrent objects
  42. [10/05/2017 12:35:05] Jaap | jpresent: They're slower, but still quite efficient
  43. [10/05/2017 12:35:09] Jaap | jpresent: Except for lists
  44. [10/05/2017 12:35:11] Jaap | jpresent: Lists suck
  45. [10/05/2017 12:35:23] Simon Asaert: I'm using a list in my thing... xD
  46. [10/05/2017 12:35:28] Jaap | jpresent: They copy themselves to a temporary list and then fill up a new list
  47. [10/05/2017 12:35:30] Jaap | jpresent: It's painful
  48. [10/05/2017 12:35:36] Jaap | jpresent: The concurrent version of the list, that is
  49. [10/05/2017 12:35:38] Simon Asaert: huh
  50. [10/05/2017 12:35:50] Jaap | jpresent: In that case, it's usually better to use a concurrent queue
  51. [10/05/2017 12:36:02] Simon Asaert: Noted
  52. [10/05/2017 12:36:15] Jaap | jpresent: NEVER USE CONCURRENTSET
  53. [10/05/2017 12:36:20] Simon Asaert: Oh?
  54. [10/05/2017 12:36:31] Jaap | jpresent: It's part of netty and sometimes doesn't remove objects when you call the remove method
  55. [10/05/2017 12:36:44] Simon Asaert: Oh? o.o
  56. [10/05/2017 12:36:47] Simon Asaert: Odd
  57. [10/05/2017 12:36:50] Jaap | jpresent: It's why our player count was fucky two days ago
  58. [10/05/2017 12:36:52] Jaap | jpresent: >.<
  59. [10/05/2017 12:36:56] Simon Asaert: Very odd
  60. [10/05/2017 12:37:32] Simon Asaert: But I'll keep that in mind
  61. [10/05/2017 12:38:15] Jaap | jpresent: Want to TS?
  62. [10/05/2017 12:38:19] Simon Asaert: sure
  63. [10/05/2017 12:38:24] Jaap | jpresent: I can show you some other stuff in relation to threads
  64. [10/05/2017 12:38:26] Simon Asaert: drag me wherever you wanna speaks :D
  65. [13/05/2017 13:47:18] Simon Asaert: If I wanted to terminate a thread outside of the thread?
  66. [13/05/2017 13:49:28] Simon Asaert: Say I have a thread that is running a video stream and the user clicks to have another video play. To have the first stream terminated, would I just have a Thread.currentThread().join which catches if it is closed?
  67. [13/05/2017 15:03:48] Jaap | jpresent: Nope
  68. [13/05/2017 15:03:56] Jaap | jpresent: You can't kill a thread
  69. [13/05/2017 15:04:03] Jaap | jpresent: The only way to kill a thread is to let it end
  70. [13/05/2017 15:26:10] Simon Asaert: Mmkay.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement