Download presentation
Presentation is loading. Please wait.
1
PA1 Discussion
2
Today Review Multithreading in Java Discuss Client-Server Architecture
Some Hints on PA1
3
Thread Concepts A program may consist of many tasks that can run concurrently. A thread is the flow of execution of a task.
4
Thread Concepts In Java, each task is an instance of the Runnable interface, also called a runnable object. A thread is essentially an object that facilitates the execution of a task.
5
Thread Concepts When your program executes as an application, the Java interpreter starts a thread for main method. You can create additional threads to run concurrent tasks. Task 1: print the letter “a” 50 times Task 2: print the letter “b” 50 times Task 3: print the integers 1 through 50 Example 1: TestThread1.java
6
Thread Concepts Another way to create a multithreading program is to define a class that extends class Thread and implements the run() method.
7
Thread Concepts We will take this approach for PA1!
Another way to create a multithreading program is to define a class that extends class Thread and implements the run method. Task 1: print letter “z” 50 times Task 2: print integers 1 through 50 Example 2: TestThread2.java
8
Thread Synchronization
A shared resource may become corrupted if it is accessed simultaneously by multiple threads. To avoid race conditions, it is necessary to prevent more than one thread from simultaneously entering a certain part of the program. The keyword synchronized: acquire a lock of an object or class first before executing a block of code or a method. Example 3: TestThread3.java
9
Part I: Writing a Client-Server Application
#status I just woke up! #statusPosted User User input: I just woke up!
10
Part II: Implementing a Broadcast Status Update Using Multi-threading
How does the server manage multiple clients? How does each user broadcast messages?
11
Part III: Adding Friendship / Multicast Capability to the Social Media App
private final userThread[] threads; ArrayList<String> friends = new ArrayList<String>(); ArrayList<String> friendrequests = new ArrayList<String>(); //keep track of sent friend requests
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.