not managed by the operating system ➔ Cannot be “interrupted”, they have to give up control ➔ After pausing, they can resume in a different thread[1] ➔ Implemented in JavaScript as Generators [1] Depending on the implementation
must divide its time into “time slices” and schedule threads to run in each time slice Time slicing Time Slice 1 Kernel Slice 2 Kernel Slice 3 Kernel Slice 4 Kernel Thread 1 Thread 2 Thread 3 Thread 1 Single CPU Core Concurrent vs parallel
x = 25.0 (stored as 0x41 c8 00 00) x = 290.0 (stored as 0x43 91 00 00) console.log(x) (prints 400.0, which is 0x43 c8 00 00) Interrupted after storing the first byte T 1 T 2
input and blocks repaint ➔ Blocking the user input for long will trigger the “Unresponsive script” alert in some browsers ➔ Performing tasks in parallel generally makes them complete faster Why would you use it? Multi-threading in JavaScript
like isolated processes ➔ Nothing is shared except ShareArrayBuffer[1] ➔ Communication only through engine-provided inter-thread communication ➔ ArrayBuffer, MessagePort, and Transferables can be transferred Understanding Web Workers/Worker Threads [1] SharedArrayBuffer is disabled in most browsers by default to prevent timing attacks
00) x = 290.0 (stored as 0x43 91 00 00) console.log(x) (prints 400.0, which is 0x43 c8 00 00) Interrupted after storing the first byte T 1 T 2 SharedArrayBuffer can put you in this situation Remember this? Creating Web Workers