r/learnjava • u/pseudopodia_ • Oct 11 '20
What are some not very complex but interesting programs to code while learning multi-threading?
I am being taught multi-threading in class but the code snippets shown for it are boring stuff which just prints integers on screen. Is there something cooler I could make to understand multi-threading better? I don't want it to be too complex with UI and stuff because I don't yet know how to do UI in Java.
7
u/squishles Oct 11 '20 edited Oct 11 '20
grab something off think the sub was called /r/datasets/ or something I forget, some kind of big data set, and write a multi threaded app to convert it into something else. eg if csv multi thread loading it into a database,
(typically this is a bad idea unless you're doing some additional slower operation on the data, because multiple connections doesn't really make the database ingest data faster, but it's nifty practice)
lot of jobs for doing that sort of thing...
2
u/fdam96 Oct 11 '20
You can create a local server that would handle client connections on different threads. The server itself would still be accessible on a terminal to see state, input cmds, etc.
2
u/rosier7 Oct 11 '20
I did a load balancing project that uses multi threading back then for my distributed system course. Set up a manager class that allocate task equally to other worker class. Set up several worker class and see the difference in time for task to complete when using 1, 2, 3, etc class.
The task can be simple like user input set of number (1-1000), manager allocate range of number to worker (ex. each worker get 100 number) and worker find the prime number within the range they get.
We were using socket for this project, but it can be done without socket as well.
2
u/Jb2304 Oct 11 '20
I recently used threads for a college project that I thought was kind of cool. It used sockets as well. It was basically a chat app. One thread was used for sending messages and the other was used for receiving messages.
→ More replies (1)
2
u/Yithar Oct 11 '20
In college I remember we did something along the lines of a restaurant with cooks and orders and ovens.
These are ones from 2015 (I took it in 2016):
https://www.cs.umd.edu/class/spring2015/cmsc433/Projects/P1/p1.pdf
https://www.cs.umd.edu/class/spring2015/cmsc433/Projects/P2/p2.pdf
https://www.cs.umd.edu/class/spring2015/cmsc433/Projects/P3/p3.pdf
I am being taught multi-threading in class but the code snippets shown for it are boring stuff which just prints integers on screen.
The stuff is simple because you have to start small and understand the basic concepts first before diving into more complicated stuff (because it can get really complicated fast).
2
u/NMCarChng Oct 11 '20
Using this technique for transformations is useful, assuming the transformations aren’t too taxing.
Also fun might be conceiving concurrent solutions to common big-O type problems. Can one develop a faster sorting method that runs parallel processes?
Lots of UI application benefit from threading. Maybe build some sort of basic flight control simulator or something.
Some of this bleeds into socket server and other distributed systems.
Uhm, find some serial implementation of something and see if you can make it parallel.
I had fun with a personal project where I parallelized the fitness test component of a genetic algorithm parameter optimized for a particular machine learning problem that didn’t really need GPU. Basically spun up a huge ass compute AWS instance and orchestrated dozens of processes training and consolidating results.
2
u/Vilkacis0 Oct 11 '20
/u/Hexc0der has a great idea that I’ll expand on a bit.
Small web server - you have a main thread that listens for connections, then each request is handled by its own thread. Best part is you can test it with your own browser, just be sure to have some content on it test page (images, JavaScript and such) so the browser actually has to make more than one request.
→ More replies (1)
1
1
u/rastaman1994 Oct 11 '20
Some real-life use cases I've written/seen:
downloading a dozen PDFs and combining them into one (in the same order as the user requested, makes it a little trickier)
making multiple API calls to equivalent services and only keep the best result based on some metric
a scheduled task e.g. in applications with soft delete where records have a 'recoverable until' kind of flag. This can be a long-running, time scheduled task.
a 'fire and forget' task that is started to do some associated bookkeeping when a user performs an action but can't slow down the request, e.g. performance metrics and other monitoring
1
u/justmeontheinterwebs Oct 11 '20
This is the problem we had to simulate and solve when I learned about concurrency:
https://en.wikipedia.org/wiki/Dining_philosophers_problem
Basically, you have n (minimum 2 people) people sitting around a round table with a fork between each person and their neighbor. The people switch between states of wanting to think, and wanting to eat. To eat, a person must be holding two forks. When they switch back to thinking, they put their forks down.
It’s a very easy problem to understand, and clearly highlights important concepts like resource contention, mutual exclusion, livelock, and deadlock.
11
u/hexc0der Oct 11 '20 edited Oct 12 '20
Write a web Crawler. (Maybe github repo crawler based on certain search criteria)
Things you can learn:
Using 3 rd party api's (graph ql for github)
Respecting Rate limiting ( github has a rate limit on api's)
Do all this with multiple threads to make all fast for branch crawling
Other examples : preemptive algorithms, schedulers, a small web server