第九节:详细讲解Java中的泛型,多线程,网络编程

前言

大家好,我是 Vic,今天给大家带来详细讲解Java中的泛型,多线程,网络编程的概述,希望你们喜欢

泛型

泛型格式:ArrayList<Type> list= new ArrayList<Type>();
ArrayList<Type> list= new ArrayList<>();
Type可以为类,接口
使用泛型可以使加入的,不用被强制

通配符

ArrayList<? extends Type> list= new ArrayList<>();
代表任意泛型

多线程

在同一时间,做多件事情.

创建线程的方法

继承类Thread并重写run(),run()称为线程体;用这种方法定义的类不能再继承其他类。

class FirstThread extends Thread{
public void run(){
 for(int i=0;i<100;i++){
  System.out.println("FirstThread"+i);
 }
}
}

class Test{
public static void main(Sting args[]){
 FirstThread ft = new FirstThread();
 ft.start();
 
 for(int i = 0; i<100;i++){
  System.out.println("main"+i):
 }
}
}

接口Runnable的类作为线程的目标对象

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

class Test{
public static void main(String args[]){
 Test test = new Test();
 Thread t = new Thread(test);
 System.out.println(t.getPriority());
 t.start();
}
}

中断线程

Thread.sleep();
Thread.yield();//让出自己正在使用的CPU

设置线程的优先级

getPriority();
setPriority();

class Test implements Runnable{
public void run(){
 for(int i = 0;i<100;i++){
  System.out.println("Runnable"+i);
  if(i==50){
   try{
    Thread.sleep(2000);
   }
   catch(Exception e){
    System.out.println(e);
   }
  }
 }
}
}

class Test{
public static void main(String args[]){
 RunnableImp1 ri = new RunnableImp1();
 Thread t = new Thread(ri);
 
 t.setPriority(Thread.MAX_PRIORITY);
 //t.setPriority(Thread.MIN_PRIORITY);
 
 t.start();
 System.out.println(t.getPriority());
 }
}


class Test{
public static void main(String args[]){
 MyThread myThread = new MyThread();

 Thread t1 = new Thread(myThread);
 Thread t2 = new Thread(myThread);

 t1.setName("线程1");
 t2.setName("线程2");

 //分别启动
 t1.start();
 t2.start();
}
}

class MyThread implements Runnable{
int i = 100;
public void run(){
 while(true){
  System.out.println(Thread.currentThread().getName()+i);
  i--;
  Thread.yield();
  if(i<0){
   break;
  }
 }
}
}

//同步代码块

class MyThread implements Runnable{
int i = 100;
public void run(){
 while(true){
  synchronized(this){
   System.out.println(Thread.currentThread().getName()+i);
   i--;
   Thread.yield();
   if(i<0){
    break;
   }
  }
 }
}
}

深入synchronized关键字

class Service{
public void fun1(){
 synchronized(this){
  try{
   Thread.sleep(3*1000);
  }
  catch(Exception e){
   System.out.println("fun1");
  }
 }
 
public void fun2(){
 synchronized(this){
  System.out.println("fun2");
 }
}
}

class MyThread1 implements Runnable{
private Service service;
public MyThread1(Service service){
 this.service = service;
}
public void run(){
 service.fun1();
}
}

class MyThread2 implements Runable{
private Service service;
public MyThread2(Service service){
 this.service = service;
}
public void run(){
 service.fun2();
}
}

class Test{
public static void main(String args[]){
 Service service = new Service();
 Thread t1=new Thread(new MyThread1(service));
 Thread t2=new Thread(new MyThread2(service));

 t1.start();
 t2.start();
}
}

同步锁 锁住的是service
同步方法,同步代码块锁住this

class Service{
public synchronized void fun1(){
 try{
  Thread.sleep(3*1000);
 }
 catch(Exception e){
  System.out.println(e);
 }
 System.out.println("fun1");
}
public void fun2(){
 synchronized(this){
  System.out.println("fun2");
}
}
}


数组

class Test{
public static void main(String args[]){
 //数组的静态声明
 int arr [] = {5,2,7,8,9,0};

 arr[3] = 10;

 //System.out.println(arr[3]);

 for(int i = 0;i<5;i++){
   System.out.println(arr[i]);
  }

 }
}

class Test{
public static void main(String args[]){
 int arr[] = {2,4,6,7,8};
 
 System.out.println(arr.length);
 }
}

数组的动态声明

class Test{
public static void main(String args[]){

//动态声明
int arr [] = new int [10];
System.out.println("arr数组长度"+arr.length);

  for(int i = 0;i<arr.length;i++){
   System.out.println(arr[i]);
  }
}
}

二维数组

class Test{
public static void main(String args[]){
 //二维数组的定义方法,长度为3

  int arr [][] = {{1,2,3},{4,5,6},{7,8,9}};
  
  System.out.println(arr[1][1]);

  for(int i = 0; i < 3; i++){
   for(int j = 0; j < 3; j++){
    System.out.println(arr[i][j]);
   }
  }

}
}

优化

class Test{
public static void main(String args[]){
 //二维数组的定义方法,长度为3

  int arr [][] = {{1,2,3},{4,5,6},{7,8}};
  
  System.out.println(arr[1][1]);

  for(int i = 0; i < arr.length; i++){
   for(int j = 0; j < arr[i].length; j++){
    System.out.println(arr[i][j]);
   }
  }

}
}

动态

class Test{
public static void main(String args[]){

  //int arr [][] = {{1,2,3},{4,5,6},{7,8}};
  
  int arr [][] = new int[3][5]; 
 
  System.out.println(arr[1][1]);

  for(int i = 0; i < arr.length; i++){
   for(int j = 0; j < arr[i].length; j++){
    System.out.println(arr[i][j]);
   }
  }

}
}

线程概念
进程:就是执行一个任务;
线程:就是在进程内部同时做的事情。

网络开发Socket和ServerSocket

Socket为“孔”或“插座”,创建Socket,打开连接Socket的输入或输出流,对Socket进行读写,关闭Socket。

Accept方法用于产生“阻塞”,这里有getInputStream方法和getOutInputStream方法,会产生一个IOException,

在Java.net包中,有Socket和ServerSocket两个类。以JDK1.6介绍:

public Socket()
public Socket(String host, int port)
//host - 主机名,或者为 null,表示回送地址
//port - 端口号

public Socket(InetAddress address,int port)
//address - IP 地址
//port - 端口号
ServerSocket(int port)

ServerSocket(int port,int backlog)

ServerSocket(int port,int backlog,InetAddress binAddr)

服务器与客户端通信

package two;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocket1 {
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   try {
     ServerSocket ss = new ServerSocket(2007);
     while(true) {
       Socket s = ss.accept();
       InputStream is = s.getInputStream();
       OutputStream os = s.getOutputStream();
       PrintStream ps = new PrintStream(os);
       ps.println("helloworld, i am server thinkpad");
       
       DataInputStream dis = new DataInputStream(is);
       String str = dis.readLine();
       System.out.println(str);
       s.close();
       
     }
   }
   catch(IOException ee) {
     System.out.println(ee);
   }
   catch(Exception e) {
     System.out.println(e);
   }
 }
}
package two;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ConnectException;
import java.net.Socket;

public class ClientSocket {
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   try {
     Socket s = new Socket("########",2007);
     InputStream is = s.getInputStream();
     OutputStream os = s.getOutputStream();
     PrintStream ps = new PrintStream(os);
     ps.println("hello , i am client");
     
     DataInputStream dis = new DataInputStream(is);
     
     String str = dis.readLine();
     System.out.println(str);
     s.close();
     
   }
   catch(ConnectException eee) {
     System.out.println(eee);
   }
   catch(IOException ee) {
     System.out.println(ee);
   }
   catch(Exception e) {
     System.out.println(e);
   }
 }
}

总结

  • 本文讲了详细讲解Java中的泛型,多线程,网络编程,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注
点赞