1 / 12

Java Network Programming 4

Dive into the essentials of Java Network Programming, focusing on the interplay between processes, multithreading, and context switching in operating systems. This guide explores various approaches to implement threading in Java, including extending the Thread class and implementing the Runnable interface. Learn how to create a simple client-server application that communicates over a specific port, handling integer inputs and demonstrating multi-threaded server processing. By the end, you’ll understand the foundation of network services and concurrent programming in Java.

Download Presentation

Java Network Programming 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Network Programming 4 352362 – Network Operating Systems and Protocols ChoopanRattanapoka

  2. MultiTasking proc1 proc2 proc3 CPU การทำงานปกติ(ง่ายๆ) ให้คิดถึง 1 application = 1 process CPU รุ่นที่มี core เดียวจะทำงานได้ทีละ 1 process เพราะฉะนั้นที่เราเห็น process หลายตัวทำงานพร้อมกันได้ จะมีระบบที่เรียกว่า context switch ที่ OS จะเลือก process ขึ้นมาใช้งานกับ CPU

  3. Multi Tasking Programming Service Process Server Process Service Process Service Process

  4. Multi Threading Server process Service Thread Service Thread Service Thread

  5. Java Thread Programming • ใน Java สามารถจะเขียน Thread ได้ 2 แบบ • ใช้ extends Thread import java.lang.*; public class Counter extends Thread { public void run() { .... } }

  6. Java Thread Programming • ใช้ implements Runnable import java.lang.*; public class Counter implements Runnable{ public void run() { .... } }

  7. Example 1 : Runnable

  8. Example 1 : Runnable (ต่อ)

  9. Example 2 : Thread

  10. Example 3 : Single Thread

  11. Example 4 : Multi Thread

  12. Exercise • จงเขียนโปรแกรม Client – Server • Server • ให้บริการที่ port หมายเลข 5432 • เป็นการทำงานแบบ multi thread • จะรับค่าตัวเลขจำนวนเต็มจาก Client แล้วจะหยุดการทำงานเป็นวินาทีที่กำหนด • จากนั้นส่งคำว่า Hello กลับ • Client • โปรแกรมจะรับจำนวนเต็ม 1 ตัวจาก command line • ติดต่อกับ Server โดยจะส่งจำนวนเต็มที่รับมาจาก command line ให้ server • Client จะรอรับข้อความจาก Server ก่อนปิดการเชื่อมต่อ • ให้เขียนทั้งแบบที่ใช้ implements Runnableและ xtends Thread

More Related