湖南工业大学论坛
欢迎来到湖南工业大学论坛,工大30000师生的交流平台!

希望你能玩得愉快!

Join the forum, it's quick and easy

湖南工业大学论坛
欢迎来到湖南工业大学论坛,工大30000师生的交流平台!

希望你能玩得愉快!
湖南工业大学论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

J***A 私塾第十四、十五章笔记整理

向下

JAVA 私塾第十四、十五章笔记整理 Empty J***A 私塾第十四、十五章笔记整理

帖子 由 luodis 周六 二月 26, 2011 2:50 pm

J***A 私塾第十四、十五章笔记整理

第十四章 多线程

线程的基本概念:
线程是一个程序内部的顺序控制流。
进程是正在执行的程序,一个或更多的线程构成了一个进程。

线程和进程的区别:
每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大。
线程作为轻量级的进程,同一类线程可以共享代码和数据空间,但每个线程有独立的运行栈和数据空间,因此线程切换的开销小。
多进程—在操作系统中同时运行多个任务(程序),也称多任务。
多线程—在同一应用程序中多个顺序流同时执行。

创建线程的2中方式:
1.定义线程实现Runnable接口
代码:

public class TestThread1 {
  public static void main(String args[]) {
      Runner1 r = new Runner1();
      Thread t = new Thread(r);
      t.start();
      for(int i=0; i<100; i++) {
        System.out.println("Main Thread:------" + i);
      }
  }
}

class Runner1 implements Runnable {
  public void run() {
      for(int i=0; i<100; i++) {
        System.out.println("Runner1 :" + i);
      }
  }
}

2.定义一个Thread的子类,并重写其Run()方法
代码:

public class TestThread1 {
  public static void main(String args[]) {
      Runner1 r = new Runner1();
      r.start();
      for(int i=0; i<100; i++) {
        System.out.println("Main Thread:------" + i);
      }
  }
}
class Runner1 extends Thread {
  public void run() {
      for(int i=0; i<100; i++) {
        System.out.println("Runner1 :" + i);
      }
  }
}

线程状态:
【此处有图,可以到J***A 私塾官网下载完整笔记:www.javass.cn】

线程控制基本方法:
[align=center]方法[/align][align=center]功能[/align]isAlive()判断线程是否还活着,即线程是否还未终止getPriority()获得线程的优先级数值setPriority()设置线程的优先级数值Thread.sleep()将当前线程睡眠,并指定毫秒数join()调用某线程的该方法,将当前线程与该线程“合并“,即等待该线程结束,再回复当前线程的运行。wait()当前线程进入对象的wait poolnotify()/notifyAll()唤醒对象的wait pool中的一个/所有等待线程[/size]

线程的优先级:
代码:

public class TestPriority {
  public static void main(String[] args) {
      Thread t1 = new Thread(new T1());
      Thread t2 = new Thread(new T2());
      t1.setPriority(Thread.NORM_PRIORITY + 3);
      t1.start();
      t2.start();
  }
}

class T1 implements Runnable {
  public void run() {
      for(int i=0; i<1000; i++) {
        System.out.println("T1: " + i);
      }
  }
}

class T2 implements Runnable {
  public void run() {
      for(int i=0; i<1000; i++) {
        System.out.println("------T2: " + i);
      }
  }
}

线程同步:
当某个对象被用synchronized来修饰时,表明该对象某一时刻只能由一个线程访问。
代码:

public class TestSync implements Runnable {
  Timer timer = new Timer();
  public static void main(String[] args) {
      TestSync test = new TestSync();
      Thread t1 = new Thread(test);
      Thread t2 = new Thread(test);
      t1.setName("t1");
      t2.setName("t2");
      t1.start();
      t2.start();
  }
  public void run(){
      timer.add(Thread.currentThread().getName());
  }
}

class Timer{
  private static int num = 0;
  public synchronized void add(String name){
      synchronized (this) {
        num ++;
        try {Thread.sleep(1);}
        catch (InterruptedException e) {}
        System.out.println(name+", 你是第"+num+"个使用timer的线程");
      }
  }
}

死锁:
代码:

public class TestDeadLock implements Runnable {
  public int flag = 1;
  static Object o1 = new Object(), o2 = new Object();
  public void run() {
      System.out.println("flag=" + flag);
      if(flag == 1) {
        synchronized(o1) {
            try {
              Thread.sleep(500);
            }catch (Exception e) {
              e.printStackTrace();
            }
            synchronized(o2) {
              System.out.println("1");
            }
        }
      }
      if(flag == 0) {
        synchronized(o2) {
            try {
              Thread.sleep(500);
            } catch (Exception e) {
              e.printStackTrace();
            }
            synchronized(o1) {
              System.out.println("0");
            }
        }
      }
  }
  public static void main(String[] args) {
      TestDeadLock td1 = new TestDeadLock();
      TestDeadLock td2 = new TestDeadLock();
      td1.flag = 1;
      td2.flag = 0;
      Thread t1 = new Thread(td1);
      Thread t2 = new Thread(td2);
      t1.start();
      t2.start();
 
  }
}

wait和sleep的区别
wait时,别的线程可以访问锁定对象。
sleep时,别的线程不可以访问锁定对象。

第十五章:网络编程

什么是url?基本的格式是?
URL(统一资源定位器,Uniform Resource Locator)用于表示Internet 上资源的地址。这里所说的资源,可以是文件、目录或更为复杂的对象的引用。
http://home.netscape.com/home/welcome.html

IP,Port,TCP的基本功能
IP代表网络位置
Port代表端口号
TCP可保证不同厂家生产的计算机能在共同网络环境下运行,解决异构网通信问题,是 目前网络通信的基本协议

Java网络模型的基本功能
描述服务端和客户端的连接过程
【此处有图,可以到J***A 私塾官网下载完整笔记:www.javass.cn】

Java网络编程究竟做些什么?如何做?
1.建立连接
2.准备输出的数据,流式输出
3.流式输入,编程业务需要的格式
4.关闭连接

服务器分配一个端口号。如果客户请求一个连接,服务器使用accept()方法打开socket 连接。
客户在host的port端口建立连接。
服务器和客户使用InputStream和OutputStream进行通信。

基于Socket编程
代码:

import java.net.*;
import java.io.*;

public class TestServer {
  public static void main(String args[]) {
      try {
        ServerSocket s = new ServerSocket(8888);
        while (true) {
            Socket s1 = s.accept();
            OutputStream os = s1.getOutputStream();
            DataOutputStream dos =
            new DataOutputStream(os);
            dos.writeUTF("Hello," +s1.getInetAddress() + "port#" +s1.getPort() + "\nb  ye!");
            dos.close();
            s1.close();
        }
      }catch (IOException e) {
        System.out.println("程序运行出错:" + e);
      }
  }
}

TCP和UDP区别
TCP能保证传输内容的完整和准确,UDP不能保证。
luodis
luodis

帖子数 : 21
注册日期 : 10-12-30

返回页首 向下

返回页首


 
您在这个论坛的权限:
不能在这个论坛回复主题