Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [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?
- [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
- [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
- [10/05/2017 12:25:39] Jaap | jpresent: Hey :)
- [10/05/2017 12:25:46] Simon Asaert: o/
- [10/05/2017 12:25:48] Jaap | jpresent: Threads are fairly easy to use
- [10/05/2017 12:25:53] Jaap | jpresent: What do you want to know about them?
- [10/05/2017 12:26:07] Simon Asaert: Just... How they work really, how to call them etc.
- [10/05/2017 12:26:36] Jaap | jpresent: new Thread(() -> {
- //Do stuff async
- }).start();
- [10/05/2017 12:26:41] Jaap | jpresent: x)
- [10/05/2017 12:26:43] Jaap | jpresent: Easy as crap
- [10/05/2017 12:26:50] Jaap | jpresent: Do you know lambda expressions?
- [10/05/2017 12:27:15] Simon Asaert: Not from the top of my head x.x
- [10/05/2017 12:27:47] Jaap | jpresent: () -> {} is the same as new Runnable() {
- @Override
- public void run() {
- }
- }
- [10/05/2017 12:27:58] Simon Asaert: Oh right yeah
- [10/05/2017 12:29:55] Jaap | jpresent: Ok, so, the basics
- [10/05/2017 12:30:05] Jaap | jpresent: A thread is an object with a runnable in it
- [10/05/2017 12:30:18] Jaap | jpresent: To start an async thread, use thread.start()
- [10/05/2017 12:30:25] Jaap | jpresent: To start a sync thread, use thread.run()
- [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)
- [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
- [10/05/2017 12:31:35] Jaap | jpresent: But I prefer to use concurrent versions of the objects
- [10/05/2017 12:31:41] Simon Asaert: logical
- [10/05/2017 12:31:59] Jaap | jpresent: Do you know about the concurrent object and atomic objects?
- [10/05/2017 12:32:09] Simon Asaert: Not atomic objects
- [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
- [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
- [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
- [10/05/2017 12:33:38] Jaap | jpresent: It's lovely
- [10/05/2017 12:33:52] Simon Asaert: That sounds super helpful
- [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
- [10/05/2017 12:34:20] Jaap | jpresent: Basically
- [10/05/2017 12:34:28] Jaap | jpresent: Yes
- [10/05/2017 12:34:42] Jaap | jpresent: If you ever see a ConcurrentModificationException
- [10/05/2017 12:34:54] Jaap | jpresent: Just use one of the atomic or concurrent objects
- [10/05/2017 12:35:05] Jaap | jpresent: They're slower, but still quite efficient
- [10/05/2017 12:35:09] Jaap | jpresent: Except for lists
- [10/05/2017 12:35:11] Jaap | jpresent: Lists suck
- [10/05/2017 12:35:23] Simon Asaert: I'm using a list in my thing... xD
- [10/05/2017 12:35:28] Jaap | jpresent: They copy themselves to a temporary list and then fill up a new list
- [10/05/2017 12:35:30] Jaap | jpresent: It's painful
- [10/05/2017 12:35:36] Jaap | jpresent: The concurrent version of the list, that is
- [10/05/2017 12:35:38] Simon Asaert: huh
- [10/05/2017 12:35:50] Jaap | jpresent: In that case, it's usually better to use a concurrent queue
- [10/05/2017 12:36:02] Simon Asaert: Noted
- [10/05/2017 12:36:15] Jaap | jpresent: NEVER USE CONCURRENTSET
- [10/05/2017 12:36:20] Simon Asaert: Oh?
- [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
- [10/05/2017 12:36:44] Simon Asaert: Oh? o.o
- [10/05/2017 12:36:47] Simon Asaert: Odd
- [10/05/2017 12:36:50] Jaap | jpresent: It's why our player count was fucky two days ago
- [10/05/2017 12:36:52] Jaap | jpresent: >.<
- [10/05/2017 12:36:56] Simon Asaert: Very odd
- [10/05/2017 12:37:32] Simon Asaert: But I'll keep that in mind
- [10/05/2017 12:38:15] Jaap | jpresent: Want to TS?
- [10/05/2017 12:38:19] Simon Asaert: sure
- [10/05/2017 12:38:24] Jaap | jpresent: I can show you some other stuff in relation to threads
- [10/05/2017 12:38:26] Simon Asaert: drag me wherever you wanna speaks :D
- [13/05/2017 13:47:18] Simon Asaert: If I wanted to terminate a thread outside of the thread?
- [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?
- [13/05/2017 15:03:48] Jaap | jpresent: Nope
- [13/05/2017 15:03:56] Jaap | jpresent: You can't kill a thread
- [13/05/2017 15:04:03] Jaap | jpresent: The only way to kill a thread is to let it end
- [13/05/2017 15:26:10] Simon Asaert: Mmkay.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement