为什么线程不能在Java中并行运行? [英] Why do threads not run in parallel in java?

查看:495
本文介绍了为什么线程不能在Java中并行运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个程序,该程序启动两个线程,其中一个线程永远循环以在端口上接收对象,然后将它们添加到线程之间共享的数组列表中,其他线程永远循环以接收命令到从共享的数组列表中删除一个对象.

I'm trying to implement a program that starts two threads one of the threads loops forever to receive objects on a port and then add them to an array list which is shared between threads, the other thread loops forever to receive command to remove an object from the array list which is shared.

这是实例化这些线程的代码

here is the code instantiating those threads

主机是主要类,包含两个类,分别是注册和注销(线程)

Host being the main class which contains two classes register and deregister (the threads)

static ArrayList<User> Clients=new ArrayList<User>();
Host.register reg=h.new register();
Thread t=new Thread(reg);
t.run();
Host.deregister dereg=h.new deregister();
Thread t1=new Thread(dereg);
t1.run();

这是线程本身

public class register implements Runnable{
    private static final int PORT = 9111;
    @Override
    public void run() {
        listen();
    }
    public void listen(){
        try {
            ServerSocket s=new ServerSocket(PORT);
            while(true){
                Socket clsocket=s.accept();
                User recUser=null;
                ObjectInputStream in = new ObjectInputStream(clsocket.getInputStream());
                recUser=(User)in.readObject();
                Clients.add(recUser);
                in.close();
                clsocket.close();
                System.out.println(Clients.size()+" reg");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class deregister implements Runnable{
    private static final int PORT = 9999;
    @Override
    public void run() {
        listen();
    }
    public void listen(){
        try {
            ServerSocket s=new ServerSocket(PORT);
            while(true){
                Socket clsocket=s.accept();
                User recUser=null;
                ObjectInputStream in = new ObjectInputStream(clsocket.getInputStream());
                recUser=(User)in.readObject();
                Clients.remove(recUser);
                in.close();
                clsocket.close();
                System.out.println(Clients.size()+" dereg");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的问题是,实际处于活动状态并正在监听对象的唯一线程是实例化的第一个线程,为什么它们实际上并未并行运行并且都在同时监听?

My problem is that the only thread actually active and listening for objects is the first on instantiated, why are they not actually running in parallel and both listening at the same time?

推荐答案

您不使用run启动线程.您以start()开头.

You don't start a thread with run. You start it with start().

t.run()仅在当前(主)线程上运行run()方法,这就是为什么未达到t1.run()的原因.

t.run() only runs the run() method on the current (main) thread, which is why t1.run() is not reached.

将其更改为t.start()t1.start().

这篇关于为什么线程不能在Java中并行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆