dream.in.java

能以不变应万变是聪明人做事的准则。万事从小事做起,积累小成功,问鼎大成功,是成功者的秘诀。

必看的5个JAVA经典实例(二)

4.用JAVA中的多线程示例生产者和消费者问题

package com.softeem.demo;

class Producer implements Runnable {
    private SyncStack stack;

    public Producer(SyncStack stack) {
        this.stack = stack;
    }

    public void run() {
        for (int i = 0; i < stack.getProducts().length; i++) {
            String product = "产品" + i;
            stack.push(product);
            System.out.println("生产了: " + product);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

class Consumer implements Runnable {
    private SyncStack stack;

    public Consumer(SyncStack stack) {
        this.stack = stack;
    }

    public void run() {
        for (int i = 0; i < stack.getProducts().length; i++) {
            String product = stack.pop();
            System.out.println("消费了: " + product);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}

class SyncStack {
    private String[] products = new String[10];
    private int index;

    public synchronized void push(String product) {
        if (index == product.length()) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        notify();
        products[index] = product;
        index++;
    }

    public synchronized String pop() {
        if (index == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        notify();
        index--;
        String product = products[index];
        return product;
    }

    public String[] getProducts() {
        return products;
    }

}

public class TestProducerConsumer {

    public static void main(String[] args) {
        SyncStack stack = new SyncStack();
        Producer p = new Producer(stack);
        Consumer c = new Consumer(stack);

        new Thread(p).start();
        new Thread(c).start();
    }
}

5.编程实现序列化的Student(sno,sname)对象在网络上的传输

package com.softeem.demo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;

class Student implements Serializable {
    private int sno;
    private String sname;

    public Student(int sno, String sname) {
        this.sno = sno;
        this.sname = sname;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "学号:" + sno + ";姓名:" + sname;
    }
}
class MyClient extends Thread {
    @Override
    public void run() {
        try {
            Socket s = new Socket("localhost", 9999);
            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            Student stu = (Student) ois.readObject();
            String msg = "客户端程序收到服务器端程序传输过来的学生对象>> " + stu;
            System.out.println(msg);
            ois.close();
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
class MyServer extends Thread {

    @Override
    public void run() {
        try {
            ServerSocket ss = new ServerSocket(9999);
            Socket s = ss.accept();
            ObjectOutputStream ops = new ObjectOutputStream(s.getOutputStream());
            Student stu = new Student(1, "赵本山");
            ops.writeObject(stu);
            ops.close();
            s.close();
            ss.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
public class TestTransfer {
    public static void main(String[] args) {
        new MyServer().start();
        new MyClient().start();
    }
}

posted on 2009-02-22 13:48 YXY 阅读(286) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: