Thực hành Hệ phân tán (Distributed System) - pdf 16

Download miễn phí Thực hành Hệ phân tán (Distributed System)



LẬP TRÌNH SOCKET VỚI TCP
 
I.MỤC TIÊU
Lập trình kết nối giữa 2 máy tính bằng socket với kỹ thuật TCP
II. NỘI DUNG
A.LÝ THUYẾT
.1.Socket và dịch vụ TCP
- Socket: Cửa giao tiếp giữa các tiến trình và giao thứcgiao vận (UCP hay TCP)
- Dịch vụTCP: Truyền các bytes tin cậy từ một tiến trình đến các tiến trình khác
- Client phải gửi yêu cầu tới server
+ Tiến trình máy chủ phải đang được thực hiện
+ Máy chủ phải mở socket (cổng) để nhận yêu cầu từ client
- Client yêu cầu server bằng cách:
+ Tạo một socket TCP trên máy
+ Chỉ rõ IP address & port number của tiến trình máy chủ
+ Khi client tạo socket: client TCP tạo liên kết tới server TCP
 



Để tải bản Đầy Đủ của tài liệu, xin Trả lời bài viết này, Mods sẽ gửi Link download cho bạn sớm nhất qua hòm tin nhắn.
Ai cần download tài liệu gì mà không tìm thấy ở đây, thì đăng yêu cầu down tại đây nhé:
Nhận download tài liệu miễn phí

Tóm tắt nội dung tài liệu:

Bản thân ngôn ngữ Java không hỗ trợ đa thừa kế. Chúng ta chỉ có thể extends từ một lớp duy nhất. Nhưng lại có thể implements cùng lúc nhiều interface. Khi mà lớp của ta đã [extends] một lớp nào đó rồi (vd : Applet), thì chỉ có thể implements
Runnable để tạo ra Thread.
- Việc extends lớp Thread có thể dẫn đến rủi ro là bạn override các method start,
stop, ... thì có thể làm cho việc tạo thread là không thể.
Với interface Runnable(cách thứ hai) : Khi muốn tạo ra một Thread. Chương trình sẽ trong sáng và dễ tìm lỗi hơn.
Method
Mô tả
getName()
Trả về tên của thread
getPriority()
Trả về quyền ưu tiên của thread.
isAlive()
Xác định thread nào đang chạy
join()
Tạm dừng cho đến khi thread kết thúc
run()
Danh mục cần thực hiện bên trong thread
sleep()
Suspends một thread. Method này cho phép bạn xác định khoảng thời gian mà thread được cho tạm dừng
start()
Bắt đầu thread.
III.BÀI TẬP
3.1. Tạo ra 1 threat và cho nó chạy cùng với threat của mang cách main(). Có nhận xét gì ?
3.2. Kiểm tra các trạng thái Suspend, resume, and stop của một tiến trình
3.3. Tạo ra 3 Threat có tên "Ha Noi", "Da Nang" và "Sai Gon". Cho các Threat này xuất hiện 10 lần một cách ngẫu nhiên. Mỗi threat khi thực hiện xong thông báo đã thực hoàn thành công việc.
3.4.
3.5. Tạo lớp Demo cho phép đưa ra 4 tên threat cùng một threat bằng cách gọi constructor của lớp MyThread. Mỗi trong số này được coi như là một threat riêng biệt. Sau đó, threat chính tạm dừng 10 giây bằng cách gọi đến cách sleep (). Trong thời gian này, các threat tiếp tục thực thi. Khi thread chính “tỉnh lại”, nó sẽ hiển thị thông báo rằng các thread chính là chấm dứt.
3.6. Tạo 2 Threat .
ThreatA : Ghi một mảng bytes vào Stream theo cơ chế Pipe
ThreatB : Đọc từ Stream các phần từ ở Stream ra màn hình
3.7.Tạo ra 5 threat, mỗi threat sau khi hoạt động được 5s, thì thông báo dừng, và threat thứ 2 hoạt động tiếp… quá trình trên thực hiện cho đến khi hệ thống đếm đủ 30s thì thu hồi tài nguyên.
3.8.Tạo ra 2 threat hiển thị chuổi ra màn hinh nhận từ bàn phím. Kiểm tra trường hợp
b. Không sử dụng đồng bộ hóa
a. Sử dụng đồng bộ hóa
3.9. Tạo 2 threat, threat thứ nhất tạo chuỗi số nguyên ngẫu nhiên, sau đó threat thứ 2 thực hiện sắp xếp dãy số đó theo thuật toán nổi bọt và xuất kết quả ra màn hình.
3.10. Sử dụng đa luồng để đọc một ma trận với N hàng và M cột từ file text và xuất ra file khác với điều kiện là một phần tử trên ma trận này là trung bình cộng của các phần tử xung quanh.
HƯỚNG DẪN
3.1
public class Main {
public static void main(String str[]) {
final Object monitor = new Object();
new Thread() {
public void run() {
try {
synchronized (monitor) {
System.out.println("Hay cho 10 seconds ...");
monitor.wait(10000);
System.out.println("Thoi gian cho doi da qua !!!");
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}.start();
}
}
3.2.
public class Main2 {
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread("MyThread");
Thread.sleep(100);
mt.suspend();
Thread.sleep(100);
mt.resume();
Thread.sleep(100);
mt.suspend();
Thread.sleep(100);
mt.resume();
Thread.sleep(100);
mt.stop();
}
}
class MyThread implements Runnable {
Thread thrd;
boolean suspended;
boolean stopped;
MyThread(String name) {
thrd = new Thread(this, name);
suspended = false;
stopped = false;
thrd.start();
}
public void run() {
try {
for (int i = 1; i < 6; i++) {
System.out.print("->");
Thread.sleep(50);
synchronized (this) {
while (suspended)
wait();
if (stopped)
break;
}
}
} catch (InterruptedException exc) {
System.out.println(thrd.getName() + " interrupted.");
}
System.out.println("\n" + thrd.getName() + " exiting.");
}
synchronized void stop() {
stopped = true;
suspended = false;
notify();
}
synchronized void suspend() {
suspended = true;
}
synchronized void resume() {
suspended = false;
notify();
}
}
public class ThreeThreadsTest {
public static void main(String[] args) {
new SimpleThread("Ha Noi").start();
//new SimpleThread("Da Nang").start();
//new SimpleThread("Sai Gon").start();
}
}
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
System.out.println("Hoan thanh tien trinh! " + getName());
}
}
3.4.
class Demo {
public static void main (String args []) {
new MyThread ("1");
new MyThread ("2");
new MyThread ("3");
new MyThread ("4");
try {
Thread.sleep (10000);
} catch (InterruptedException e) {
System.out.println("Exception: Thread maininterrupted.");
}
System.out.println("Terminating thread: main thread.");
}
}
class MyThread implements Runnable {
String tName;
Thread t;
MyThread (String threadName) {
tName = threadName;
t = new Thread (this, tName);
t.start();
}
public void run() {
try {
System.out.println("Thread: " + tName );
Thread.sleep(2000);
} catch (InterruptedException e ) {
System.out.println("Exception: Thread "+ tName + " interrupted");
}
System.out.println("Terminating thread: " + tName );
}
}
3.6
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedBytes extends Object {
public static void writeStuff(OutputStream rawOut) {
try {
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(rawOut));
int[] data = { 82, 105, 99, 104, 97, 114, 100, 32, 72, 121, 100,
101 };
for (int i = 0; i < data.length; i++) {
out.writeInt(data);
}
out.flush();
out.close();
} catch (IOException x) {
x.printStackTrace();
}
}
public static void readStuff(InputStream rawIn) {
try {
DataInputStream in = new DataInputStream(new BufferedInputStream(rawIn));
boolean eof = false;
while (!eof) {
try {
int i = in.readInt();
System.out.println("Vua doc xong: " + i);
} catch (EOFException eofx) {
eof = true;
}
}
System.out.println("Doc tat ca du lieu tu Pipe");
} catch (IOException x) {
x.printStackTrace();
}
}
public static void main(String[] args) {
try {
final PipedOutputStream out = new PipedOutputStream();
final PipedInputStream in = new PipedInputStream(out);
Runnable runA = new Runnable() {
public void run() {
writeStuff(out);
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
Runnable runB = new Runnable() {
public void run() {
readStuff(in);
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
} catch (IOException x) {
x.printStackTrace();
}
}
}
3.8.
a.
class Parentheses {    void display(String s) {    System.out.print ("(" + s);    try {       Thread.sleep (1000);    } catch (InterruptedException e) {         System.out.println ("Interrupted");    }    System.out.println(")");   } } class MyThread implements Runnable {    String s1;    Parentheses p1;    Thread t;    public MyThread (Parentheses p2, String s2) {       p1= p2;       s1= s2;       t = new Thread(this);       t.start();    }    public void run() {      p1.display(s1);    } }    public static void main (String args[]) {      Parentheses p3 = new Parentheses();      MyThread name1 = new MyThread(p3, "Bob");      MyThread name2 = new MyThread(p3, "Mary");      try {         name1.t.join();         name2.t.join();      } catch (InterruptedException e ) {           System.out.println( "Interrupted");      }   } }
b.
class Parentheses {    synchronized void display(String s) {    System.out.print ("(" + s);    try {       Thread.sleep (1000);    } catch (InterruptedException e) {         System.out.println ("Interrupted");    }    Syst...
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status