views:

553

answers:

8

I have been programming in java for a while (5 years), and I think that I have a good understanding of most of the aspects of the language. However, I think I haven't worked with threads as much as I would like to.

What would be a good small project to learn java threads deeply?

Any recommendations?

Thanks

+2  A: 

Write a web crawler !!

Tom
+7  A: 

A client server app :

the server accepts connections on a socket and create a communication thread for each client.

this could be a game, or a task manager or anything you want.

Edit : i developped an eMule client like this during my internship, it is easy to do, and you can explore all the aspects of thread programmation.

Maxime ARNSTAMM
+1 - a classic project. You'll find a lot of snippets, tutorials and help on the net (and on SO).
Andreas_D
Sounds like a good idea to add the online multiplayer feature to the games I have coded in the last 2-3 months (pacman, checkers, tetris, etc).I will try to do this on Pac-Man, maybe one player controller for each ghost? (will be boring probably, but at least I will use threads deeply I guess)
davidrobles
Give some thought to how you are going to test the multi-threadedness of it; in other words, making sure that you got it right. Being able to use a tool like JMeter to exercise the server may help expose some issues.
VoiceOfUnreason
+3  A: 

An excellent way to start is by implementing the Producer/Consumer problem. This lets you deal with starting up multiple threads, synchronization, locking, and dealing with how to exit thread execution. Once you've done that you will should have a good idea about Java threading (and threading in general).

gab
+5  A: 

I learned threads by writing an elevator simulation. Each thread is an object representing an elevator in a building. A separate structure contains "what buttons have been pressed on the floors?"

It might not be a practical use - it's no webcrawler - but it's an hour or two, and you will certainly have a much better grasp of threads and synchronization when you're done. If you want to spend a lot more time fooling with this, I might still try something very simple first, to get your feet wet.

Dean J
davidrobles
@davidrobles: I've never read the full Java Deitel2 book, and it's been many years since I've seen their C++ one, but yeah, wouldn't be surprised at all if it's in there.
Dean J
+2  A: 

I wrote a java version of PacMan. PacMan was on his own thread, as was each Ghost. Each ghost had its own algorithm for finding PacMan.

San Jacinto
I wrote a Pac-Man as well, but I never used threads! Just one for the whole game. I will try to see how I can put threads there.
davidrobles
+3  A: 

Try developing a Instant Message System where multiple user connect to a single server. The exerpt below is taken from Dietel and Dietel - Java How to Program 6th Edition

Chat rooms have become common on the Internet. They provide a central location where users can chat with each other via short text messages. Each participant can see all messages that the other users post, and each user can post messages.

With this exercise you can put your 5 years of experience to the test as this small project requires not only multithreading, but networking and GUI. In addition you need to use a technique called multicasting.

Also, open source projects making extensive use of multithreading is another good place to get inspiration and see real life examples. From there, you can participate or start your own open source project.

All the best!

Julian
+1  A: 

As a first step, consider implementing the Dining Phisolophers problem. That will introduce you to various ways to start threads, define partitioned jobs, and share data among threads.

There are many "correct" solutions and many more dangerous attempts. Start mining the java.util.concurrent packages early. Write it wrong and explore what it takes to make it correct.

seh
+1  A: 

Write a P2P chat software with TCP. You will need to know about Java Networking classes and Streams too, but it's not too tough. And learning the java.net package is worth the effort!

ErJab