Java开发服务器的线程怎么处理?

Java开发服务器的线程怎么处理?,第1张

在进行服务器处理的过程中,需要保证数据的正确处理,那么最重要的就是使用不同的数据处理模式进行运算。在整个过程中,可能很多人对服务器的知识并不了解,那么应该如何进行Java开发服务器的线程处理呢,关于线程处理有哪些知识?下面北京北大青鸟为大家介绍关键服务器线程处理的简单知识。

1、BIO线程模型

在JDK14中引入JavaNIO之前,所有基于Java的Socket通信都使用了同步阻塞模式(BIO)。这种请求-响应通信模型简化了上层的应用程序开发上,但在具有性能和可靠性的情况下,存在一个巨大的瓶颈。在一段时间里面,大型应用程序服务器主要是用C或C++开发的,因为它们可以直接使用 *** 作系统提供的异步I/O或AIO功能。

当流量增加且响应时间延迟增加时,JavaBIO开发的服务器软件只能通过硬件的不断扩展来满足并发性和低延迟的情况,这极大地增加了企业的成本和群集大小。系统的不断扩展,系统的可维护性也面临着巨大的挑战,只能通过购买性能更高的硬件服务器来解决问题,这将导致恶性循环的产生。

2、异步非阻塞线程模型

从JDK10到JDK13,Java的I/O类库非常原始。UNIX网络编程中的许多概念或接口未反映在I/O类库中,例如Pipe、Channel、Buffer和Selector等。在发布JDK14的时候,NIO正式发布JDK作为JSR-51。并且它还添加了一个javanio包,为异步I/O开发提供了许多API和库。

3、RPC性能三原则

影响RPC的性能主要有三大元素,其中主要为I/O模型、协议及线程。

I/O模型:使用什么样的通道传递给另一方,BIO,NIO或AIO发送数据,IO模型在很大程度上能够决定框架的性能。

协议:应该使用什么样的通信协议,Rest+JSON或基于TCP的专用二进制协议。参加电脑培训的过程中发现,协议的选择不同,性能模型也不同。内部专用二进制协议的性能通常可以比公共协议更好地设计。

线程:如何读取数据报?在执行读取后的编解码器的哪个线程中,如何分发编码消息,通信线程模型是不同的,并且对性能的影响也非常大。

class test {
private static int s = 2000;
public synchronized static void sub(int m){//自定义方法,可以类比于自定义异常
int temp = s;
temp = temp - m;
try {
Threadsleep((int)(Mathrandom() 1000));
}catch (Exception e){
Systemoutprintln(egetMessage());
}
s = temp;
Systemoutprintln("s = " + s);
}
}
class Customer extends Thread{
public void run(){//调用run()方法,运行run()方法中的程序片
for(int i = 1;i <= 4;i++){
testsub(100);
//当线程cus1没有结束对sub方法的使用之前,cus2无法进入并运行此方法
//synchronized的作用就在于此
}
}
}
public class Synchronizedtest{
public static void main(String[] args){
Customer cus1 = new Customer();
Customer cus2 = new Customer();
cus1start();
cus2start();
}
}
自己研究下吧
线程据我所知在开发大型项目的时候才会用到

创建线程,就是这样
extends Thread 或者 implements Runnable,但是有很多问题;

所以引申出了下面的线程池
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,
若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,
保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

//修改了,添加了start、stop方法、并且在run方法中添加判断。。。
import javaawt;
import javaxswing;
public class test1 {
    public static void main(String[] args) {
        JFrame frm = new JFrame("Java Farme");
        frmsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
        ball B1 = new ball(50, 50);
        frmadd(B1);
        B1start();
        frmsetBounds(400, 200, 700, 400);
        frmsetVisible(true);
    }
}
class ball extends JPanel implements Runnable {
    int x;
    int y;
    Thread thread;
    public ball(int x, int y) {
        thisx = x;
        thisy = y;
    }
    public void paint(Graphics g) {
        superpaint(g);
        gsetColor(Colorblue);
        gfillOval(x, y, 40, 40);
        gdispose();
    }
    public void start() {
        if (null == thread) {
            thread = new Thread(this);
            threadstart();
        }
    }
    public synchronized void stop() {
        if (null != thread) {
            threadinterrupt();
            thread = null;
            notifyAll();
        }
    }
    public void run() {
        Thread me = ThreadcurrentThread();
        for (; me == thread;) {
            if (x < 400) {
                x += 50;
                thisrepaint();
            } else {
                x = 0;
                thisrepaint();
            }
            try {
                Threadsleep(100);
            } catch (InterruptedException ex) {}
        }
    }
}

Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例。Java可以用三种方式来创建线程,如下所示:
1)继承Thread类创建线程
2)实现Runnable接口创建线程
3)使用Callable和Future创建线程

(1)通过扩展Thread类来创建多线程

public static void main(String [] args){
        MutliThread m1=new MutliThread("Window 1");
        MutliThread m2=new MutliThread("Window 2");
        MutliThread m3=new MutliThread("Window 3");
        m1start();
        m2start();
        m3start();
    }
}
class MutliThread extends Thread{
    private int ticket=100;//每个线程都拥有100张票
    MutliThread(String name){
        super(name);//调用父类带参数的构造方法
    }
    public void run(){
        while(ticket>0){
            Systemoutprintln(ticket--+" is saled by "+ThreadcurrentThread()getName());
        }
    }
}

(2)通过实现Runnable接口来创建多线程

 public static void main(String [] args){
        MutliThread m1=new MutliThread("Window 1");
        MutliThread m2=new MutliThread("Window 2");
        MutliThread m3=new MutliThread("Window 3");
        Thread t1=new Thread(m1);
        Thread t2=new Thread(m2);
        Thread t3=new Thread(m3);
        t1start();
        t2start();
        t3start();
    }
}
class MutliThread implements Runnable{
    private int ticket=100;//每个线程都拥有100张票
    private String name;
    MutliThread(String name){
        thisname=name;
    }
    public void run(){
        while(ticket>0){
            Systemoutprintln(ticket--+" is saled by "+name);
        }
    }
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13342209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-18
下一篇 2023-07-18

发表评论

登录后才能评论

评论列表(0条)

保存