学习Java线程的小项目

时间:2023-01-17 11:24:08

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.

我已经在java中编程了一段时间(5年),我认为我对该语言的大多数方面都有很好的理解。但是,我认为我没有像我想的那样使用线程。

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

什么是一个很好的小项目来深入学习java线程?

Any recommendations?

Thanks

8 个解决方案

#1


19  

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.

编辑:我在实习期间开发了这样的eMule客户端,很容易做到,你可以探索线程编程的所有方面。

#2


8  

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).

一个很好的开始方式是实现生产者/消费者问题。这使您可以处理启动多个线程,同步,锁定以及处理如何退出线程执行的问题。完成后,您将对Java线程(以及一般的线程)有一个很好的了解。

#3


8  

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.

我写了一个java版的PacMan。 PacMan与他自己的线程一样,每个Ghost都是如此。每个幽灵都有自己的寻找PacMan的算法。

#4


7  

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.

它可能不是一个实际的用途 - 它不是网络浏览器 - 但它是一两个小时,当你完成时你肯定会更好地掌握线程和同步。如果你想花更多的时间愚弄这个,我可能仍然会先尝试一些非常简单的事情,让你的脚湿透。

#5


4  

Write a web crawler !!

写一个网络爬虫!!

#6


3  

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

尝试开发一个即时消息系统,其中多个用户连接到单个服务器。下面的内容摘自Dietel和Dietel - Java如何编程第6版

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.

通过本练习,您可以将5年的经验用于测试,因为这个小项目不仅需要多线程,还需要网络和GUI。此外,您需要使用一种称为多播的技术。

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!

祝一切顺利!

#7


3  

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.

作为第一步,考虑实施Dining Phisolophers问题。这将向您介绍各种方法来启动线程,定义分区作业以及在线程之间共享数据。

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.

有许多“正确”的解决方案和许多更危险的尝试。尽早开始挖掘java.util.concurrent包。写错了,并探索如何使其正确。

#8


3  

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!

用TCP编写P2P聊天软件。您还需要了解Java Networking类和Streams,但这并不太难。学习java.net包是值得的!

#1


19  

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.

编辑:我在实习期间开发了这样的eMule客户端,很容易做到,你可以探索线程编程的所有方面。

#2


8  

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).

一个很好的开始方式是实现生产者/消费者问题。这使您可以处理启动多个线程,同步,锁定以及处理如何退出线程执行的问题。完成后,您将对Java线程(以及一般的线程)有一个很好的了解。

#3


8  

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.

我写了一个java版的PacMan。 PacMan与他自己的线程一样,每个Ghost都是如此。每个幽灵都有自己的寻找PacMan的算法。

#4


7  

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.

它可能不是一个实际的用途 - 它不是网络浏览器 - 但它是一两个小时,当你完成时你肯定会更好地掌握线程和同步。如果你想花更多的时间愚弄这个,我可能仍然会先尝试一些非常简单的事情,让你的脚湿透。

#5


4  

Write a web crawler !!

写一个网络爬虫!!

#6


3  

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

尝试开发一个即时消息系统,其中多个用户连接到单个服务器。下面的内容摘自Dietel和Dietel - Java如何编程第6版

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.

通过本练习,您可以将5年的经验用于测试,因为这个小项目不仅需要多线程,还需要网络和GUI。此外,您需要使用一种称为多播的技术。

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!

祝一切顺利!

#7


3  

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.

作为第一步,考虑实施Dining Phisolophers问题。这将向您介绍各种方法来启动线程,定义分区作业以及在线程之间共享数据。

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.

有许多“正确”的解决方案和许多更危险的尝试。尽早开始挖掘java.util.concurrent包。写错了,并探索如何使其正确。

#8


3  

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!

用TCP编写P2P聊天软件。您还需要了解Java Networking类和Streams,但这并不太难。学习java.net包是值得的!