Head First Java 15 网络联机

2016-08-10  本文已影响0人  促集

网络联机

Socket

Socket chatSocket = new Socket("123.1.1.1", 5000);
PrintWriter writer = new PrintWriter(chatSocket.getOutputStream());
writer.println("message to send");
writer.print("another message");
BufferedReader reader = new BufferedReader(new InputStreamReader(chatSocket.getInputStream()));
String message = reader.readLine();
import java.io.*;
import java.net.*;
public class DailyAdviceClient {
  public void go() {
    try {
      Socket s = new Socket(“127.0.0.1”, 4242);
      InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
      BufferedReader reader = new BufferedReader(streamReader);
      String advice = reader.readLine();
      System.out.println(“Today you should: “ + advice);
      reader.close();
    }
    catch(IOException ex) {
      ex.printStackTrace();
    }
  }
  public static void main(String[] args) {
    DailyAdviceClient client = new DailyAdviceClient();
    client.go();
  }
}

import java.io.*;
import java.
public class DailyAdviceServer {
  String[] adviceList = {“Take smaller bites”, “Go for the tight jeans. No they do NOT make you look fat.”, “One word: inappropriate”, “Just for today, be honest. Tell your boss what you *really* think”, “You might want to rethink that haircut.”};
  public void go() {
    try {
      ServerSocket serverSock = new ServerSocket(4242);
      while(true) {
        Socket sock = serverSock.accept();
        PrintWriter writer = new PrintWriter(sock.getOutputStream());
        String advice = getAdvice();
        writer.println(advice);
        writer.close();
        System.out.println(advice);
      }
    }
    catch(IOException ex) {
      ex.printStackTrace();
    }
  } 
  // close go
  private String getAdvice() {
    int random = (int) (Math.random() * adviceList.length);
    return adviceList[random];
  }

  public static void main(String[] args) {
    DailyAdviceServer server = new DailyAdviceServer();
    server.go();
  }
}

线程(Thread)

public class RunThreads implements Runnable {
  public static void main(String[] args) {
    RunThreads runner = new RunThreads();
    Thread alpha = new Thread(runner);
    Thread beta = new Thread(runner);
    alpha.setName(“Alpha thread”);
    beta.setName(“Beta thread”);
    alpha.start();
    beta.start();
  }
  public void run() {
    for (int i = 0; i < 25; i++) {
      String threadName = Thread.currentThread().getName();
      System.out.println(threadName + “ is running”);
    }
  }
}

并发性(Concurrency)

public class MainActivity extends AppCompatActivity {

    private Count count = new Count();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread alpha = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("alpha thread", "run: ");
                alpahGo();
            }
        });

        Thread beta = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("beta thread", "run: ");
                thetaGo();
                betaGo();
            }
        });

        alpha.start();
        beta.start();


    }

    public synchronized void alpahGo() {
        try {
            Log.i("alpha thread", "alpha sleep 6s");
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count.add(10);
        Log.i("alpha thread", count.countNumber + "alpha");
    }

    public synchronized void betaGo() {
        try {
            Log.i("beta thread", "beta sleep 3s");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count.add(10);
        Log.i("beta thread", count.countNumber + "beta");
    }

    public void thetaGo() {
        try {
            Log.i("beta thread", "theta sleep 2s");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count.add(10);
        Log.i("beta thread", count.countNumber + "theta");
    }
}

//运行结果
08-08 23:00:48.376 9810-10183/com.cuji.threadtest I/alpha thread: run: 
08-08 23:00:48.376 9810-10183/com.cuji.threadtest I/alpha thread: alpha sleep 6s
08-08 23:00:48.377 9810-10184/com.cuji.threadtest I/beta thread: run: 
08-08 23:00:48.377 9810-10184/com.cuji.threadtest I/beta thread: theta sleep 2s
08-08 23:00:50.377 9810-10184/com.cuji.threadtest I/beta thread: 10theta
08-08 23:00:54.377 9810-10183/com.cuji.threadtest I/alpha thread: 20alpha
08-08 23:00:54.379 9810-10184/com.cuji.threadtest I/beta thread: beta sleep 3s
08-08 23:00:57.379 9810-10184/com.cuji.threadtest I/beta thread: 30beta

//当只把两个调用synchronized之后,效果相同
            synchronized (this) {
                Thread.sleep(6000);
                count++;
            }
08-08 23:39:52.848 1210-2328/com.cuji.threadtest I/alpha thread: run: 
08-08 23:39:52.848 1210-2328/com.cuji.threadtest I/alpha thread: alpha sleep 6s
08-08 23:39:52.848 1210-2329/com.cuji.threadtest I/beta thread: run: 
08-08 23:39:52.848 1210-2329/com.cuji.threadtest I/beta thread: theta sleep 2s
08-08 23:39:54.849 1210-2329/com.cuji.threadtest I/beta thread: 1theta
08-08 23:39:58.849 1210-2328/com.cuji.threadtest I/alpha thread: 2alpha
08-08 23:39:58.850 1210-2329/com.cuji.threadtest I/beta thread: beta sleep 3s
08-08 23:40:01.851 1210-2329/com.cuji.threadtest I/beta thread: 3beta
上一篇下一篇

猜你喜欢

热点阅读