#MOOC help java course
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
here a way to test out of certain sections or topics in the MOOC course? I don't want to waste time going over material I already understand. Thank you for any help or advice you can provide.
You can't practice the later exercises without attempting some proportion of the earlier ones.
I don't recall if that also limits the sections you can see.
ok
They likely mean Inheritance, Polymorphism, Encapsulation and Data Abstraction.
huh
only the first of those is unique to OOP languages
so its a strange set of pillars
idk i just heard that its useful for concurrency
what is
oop
well, there is an explanation of OOP for which that is true
and an explanation/view for which it is not
whatre like the important oop stuff for concurrency
i'd say "OOP" as defined by "what java's mechanisms let you do" is not particuarly good for concurrent programs
fr??
well its not particuarly bad either
and java has really good concurrent libraries and code out there
yea i wanna build a chating thing with sockets and stuff so im trynalearn that
but if you drew a circle around all the "things you would describe as OOP", the circle of "things that make concurrency easier" would intersect, but still be a distinct other circle
the key for concurrent programs is isolation
example:
I know, but they are often called the 4 key concepts.
int x = 0;
void main() throws Exception {
var t1 = Thread.startVirtualThread(() -> {
x++;
System.out.println(x);
});
var t2 = Thread.startVirtualThread(() -> {
x++;
System.out.println(x);
});
t1.join();
t2.join();
}
here we have a program
yea
its going to do two thing concurrently
both threads are going to try and increment X
so quiz: what is the value of x at the end of this program?
it would be 1
wait how
ye
lets pretend we didn't have threads here for a minute:
int x = 0;
void main() throws Exception {
x++;
System.out.println(x);
x++;
System.out.println(x);
}
what is x at the end of this program
1 then 2
right so at the end its 2
yea
now whats different about the threaded version is
each thread is going to go through these steps
* Load value of x
* Add one to the value
* Store the value in x
so it looks like
Thread 1: Load value of x
Thread 1: Add one to the value
Thread 1: Store the value in x
Thread 2: Load value of x
Thread 2: Add one to the value
Thread 2: Store the value in x
oh yea
but, because threads run concurrently
so for thread 1 and thread 2 its 1 for both
the steps that thread 1 and 2 take can be interleaved
Thread 1: Load value of x
Thread 2: Load value of x
Thread 1: Add one to the value
Thread 1: Store the value in x
Thread 2: Add one to the value
Thread 2: Store the value in x
so while the first order ends the program with 2
ohhh
this one ends the program with 1
the other ends with it being 1
or something else wacky: the JVM is allowed to do some nonsense under race conditions
but probably 1 or 2
i think
so thats the main problem with concurrent code
wait no it would be 1 because adding 1 to thread 1 wont link to thread2
if two concurrent processes share a resource - in this case the variable x but you can imagine other stuff
then they need to coordinate over sharing that resource
so it would increment before being stored
the ways to address this are
- Share fewer/no things
If you don't have stuff you are sharing, no need to worry about coordination
- Share things that don't change
ohh
I.E. if you pass the number 5 to a program, you don't need to worry about what other parts of the program are doing to that int
yeah
same thing goes for objects which are immutable
- Coordinate sharing
now we get into stuff like locks
and atomics
locks let you make code "line up" and access a resource one at a time
atomics make some stuff be one step
AtomicInteger x = new AtomicInteger(0);
void main() throws Exception {
var t1 = Thread.startVirtualThread(() -> {
x.getAndIncrement();
System.out.println(x);
});
var t2 = Thread.startVirtualThread(() -> {
x.getAndIncrement();
System.out.println(x);
});
t1.join();
t2.join();
}
in this program in the end its always 2
because it turns what would otherwise be 3 steps
Thread 1: Load value of x
Thread 1: Add one to the value
Thread 1: Store the value in x
into one step
Thread 1: Load value of x and add one and store that value
atomics work because there is special CPU support for some things
ohh
but yeah - thats why I don't think that "OOP is good for concurrency" is quite right
you can write correct concurrent programs in imperative "OOP" languages
but in a lot of ways functional languages (which don't have much more controlled mutation) have an easier time of it
and when you come back to Java after using a language like that, you'll notice that a lot of what helps you write a concurrent program is just emulating those languages
(to some extent)
ah okay
do you know any good resources to learn this stuff in more detail
i want to learn about threads and network prgramming
Java Concurrency in Practice
another strategy to learn is to branch out
learn non java things
a lot of them will put concepts like this more front and center
like Erlang/Elixir uses the actor model for concurrency
ohh
C - no, unless you really want to understand networking code at a low level and plan to dig into that aspect specifically
rust - yeah, probably
rust puts a lot of stuff into its type system, including mutability and whether something can be sent to or shared with another thread
like this java program
oo
int x = 0;
void main() throws Exception {
var t1 = Thread.startVirtualThread(() -> {
x++;
System.out.println(x);
});
var t2 = Thread.startVirtualThread(() -> {
x++;
System.out.println(x);
});
t1.join();
t2.join();
}
it would be impossible to write in rust
two threads cannot both have mutable access to the same thing
so by proxy while learning rust you will learn a lot of this stuff