#MOOC help java course

1 messages · Page 1 of 1 (latest)

slim oasis
#

Hi, I'm going back to learn about 4 pillars of oop and i was wondering if there is a way to skip completing some parts because some of the stuff i already know pretty well. Is there a way to access all the parts?

odd fernBOT
#

<@&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>.

#
TJ-Bot
Hi, I'm going back to learn about 4 pillars of oop and i was wondering if there is a way to skip completing some parts because some of the stuff i already know pretty well. Is there a way to access all the parts?

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.

amber juniper
#

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.

slim oasis
#

ok

stable pawn
#

There are no pillars of oop

#

not sure how mooc phrases it i guess

cunning depot
stable pawn
#

only the first of those is unique to OOP languages

#

so its a strange set of pillars

slim oasis
#

idk i just heard that its useful for concurrency

stable pawn
#

what is

slim oasis
#

oop

stable pawn
#

well, there is an explanation of OOP for which that is true

#

and an explanation/view for which it is not

slim oasis
#

whatre like the important oop stuff for concurrency

stable pawn
#

i'd say "OOP" as defined by "what java's mechanisms let you do" is not particuarly good for concurrent programs

stable pawn
#

well its not particuarly bad either

#

and java has really good concurrent libraries and code out there

slim oasis
#

yea i wanna build a chating thing with sockets and stuff so im trynalearn that

stable pawn
#

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:

cunning depot
stable pawn
#
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

slim oasis
#

yea

stable pawn
#

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?

stable pawn
#

or 2

#

you don't know

slim oasis
#

wait how

stable pawn
#

well you have two threads

#

both are trying to add one

slim oasis
#

ye

stable pawn
#

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

slim oasis
#

1 then 2

stable pawn
#

right so at the end its 2

slim oasis
#

yea

stable pawn
#

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
slim oasis
#

oh yea

stable pawn
#

but, because threads run concurrently

slim oasis
#

so for thread 1 and thread 2 its 1 for both

stable pawn
#

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

slim oasis
#

ohhh

stable pawn
#

this one ends the program with 1

slim oasis
#

the other ends with it being 1

stable pawn
#

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

slim oasis
#

wait no it would be 1 because adding 1 to thread 1 wont link to thread2

stable pawn
#

if two concurrent processes share a resource - in this case the variable x but you can imagine other stuff

slim oasis
#

ohh wait

#

its a shared variable or resource

stable pawn
#

then they need to coordinate over sharing that resource

slim oasis
#

so it would increment before being stored

stable pawn
#

the ways to address this are

#
  1. Share fewer/no things
#

If you don't have stuff you are sharing, no need to worry about coordination

#
  1. Share things that don't change
slim oasis
#

ohh

stable pawn
#

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

slim oasis
#

yeah

stable pawn
#

same thing goes for objects which are immutable

#
  1. 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

slim oasis
#

oh yeah locks ive used

#

i havent used atomic tho

stable pawn
#
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

slim oasis
#

ohh

stable pawn
#

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)

slim oasis
#

ah okay

#

do you know any good resources to learn this stuff in more detail

#

i want to learn about threads and network prgramming

stable pawn
#

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

slim oasis
stable pawn
#

clojure has controlled mutation through atoms

#

haskell exists

#

many options

slim oasis
#

what about C or rust

#

will those help

stable pawn
#

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

slim oasis
#

oo

stable pawn
#
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

slim oasis
#

ohh

#

okay thanks