首页 技术 正文
技术 2022年11月11日
0 收藏 780 点赞 2,373 浏览 5771 个字

前言

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

泛型

泛型格式:ArrayList list= new ArrayList();

ArrayList 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知识点,有兴趣可以继续关注
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297