Download presentation
Presentation is loading. Please wait.
Published byHartanti Pranata Modified over 6 years ago
1
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1. You are required to write a chatting program, but you need to build some extra functions on top of your project 1. In project 1, the sever can only connect with one client at a time, but in project 2, the server should be able to connect with multiple clients at the same time. In project 1, when Alice chats with Bob, each of them can only send one message at a time. In project 2, we will extend the function to allow each client to send multiple messages. All these new functions require us to introduce the concept of threads. 11/29/2018
2
Process and Thread A process is an instance of a running program. Different processes do not share resources. A thread is a component of a process. Multiple threads can exist within the same process and share resources such as memory, and they can run in parallel. So what is thread? What is the difference between a process and a thread? 11/29/2018
3
Two ways to define and start a thread
Provide a Runnable object public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); There are two different ways to define and start a thread in Java. The first way is to provide a runnable object to the thread constructor, and a thread object will be returned. Then you can … 11/29/2018
4
Two ways to define and start a thread
Inherit Thread class, and overload the run() method public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); The second is to subclass thread, and overload the run method. Then you can create thread objects from the subclass. Still, you use the start method to start a thread. 11/29/2018
5
Sample Code Server.java Client.java
These are sample code of client and sever using multiple threads. 11/29/2018
6
How to create a sending thread and a receiving thread?
public class CommunicationThread extends Thread { private int mode; //determines it is a receiving thread or a sending thread public void run() if(mode == 0) always receive message; if(mode == 1) always send messages; } In order to enable a client to send and receive multiple messages each time, we need create a sending thread and a receiving thread for each client. How can we achieve that? Here is a hint 11/29/2018
7
Demo Video demo.mp4 Finally, this is the demo video for this project. It shows what we expect to see during your project demo. 11/29/2018
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.