前言
大家好,給大家帶來詳細講解Java中的泛型,多線程,網絡編程的概述,希望你們喜歡
泛型
泛型格式:ArrayList
通配符
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 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知識點,有興趣可以繼續關注
閱讀更多 java高級面試 的文章