简体   繁体   English

即使我正在处理另一个,UI线程也会卡住

[英]UI Thread gets stuck even though I'm working on another

I have this code that on a click of a button a service will be started from a new thread and the service will start my TCP Client. 我有这段代码,单击按钮后,将从新线程启动服务,该服务将启动我的TCP客户端。

The TCP client, once connected will send a count down event to inform the fragment to continue working. 一旦连接,TCP客户端将发送一个倒计时事件,以通知该片段继续工作。

the code looks like this 代码看起来像这样

   connectButton = (Button) view.findViewById(R.id.connectBT);
        connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            // ***** IntentService Way ****** //
                            Intent intent = new Intent(getActivity(), NetworkService.class);
                            getActivity().startService(intent);

                            // Once client is connected, give the server some info about client
                            mCountDown.await(3000, TimeUnit.MILLISECONDS);

                            String json = jsonMaker.toJson(new DeviceInfo());
                            DispatchToServer(json);
                        } catch (Exception ie) {
                            ie.getMessage();
                        }
                    }
                }).run();

but when I click connect, my UI still freezes for some reason, even though I'm waiting on a different thread then the UI thread. 但是当我单击connect时,即使我在等待与UI线程不同的线程,UI仍然由于某些原因冻结。 Same code worked fine when I used an AsyncTask instead of the service. 当我使用AsyncTask代替服务时,相同的代码工作正常。

You have written 你写

new Thread(new Runnable() {...}).run();

Thread#run is just the Thread class's implementation of the Runnable interface. Thread#run只是Runnable接口的Thread类的实现。 All it does is delegate to the run method of the Runnable you have passed to the constructor. 它所做的只是委托给您传递给构造函数的Runnablerun方法。

Java threads are started with the start method: Java线程使用start方法start

new Thread(new Runnable() {...}).start();

尝试调用thread.start()而不是thread.run()

使用Thread.start()而不是Thread.run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我正在调用 thread.interrupt(),线程也不会中断 - Thread not interrupting even though I'm calling thread.interrupt() Android UI挂在JDBC连接上-即使连接在另一个线程上 - Android UI Hangs on JDBC Connection - Even though Connection is on another Thread 即使我使用异步线程(主UI),主线程也进行了大量工作 - Main thread doing too much work even though I use an async thread (laggy UI) 线程执行太多次并导致竞争条件,即使我正在使用锁 - Thread executes too many times and causes race condition even though I'm using locks 线程卡在Thread.join()中,即使其他线程已终止 - Thread stuck in Thread.join(), even though other thread has terminated 即使在另一个线程上调用了subscribeOn(),Observable也会在主线程上运行 - Observable runs on main thread even though subscribeOn() is called on another thread 即使配置的限制为 50,log4j 仍停留在最多 13 个文件? - log4j gets stuck at a maximum number of 13 files even though the configured limit is 50? 控制台文本输出被阻止,即使它在另一个线程上 - Console text output blocked, even though it's on another thread firebase StorageException 被打印,即使我处理它 - firebase StorageException gets printed, even though I handle it 即使定义了超时,Postgres JDBC线程也会停留在java.net.SocketInputStream.socketRead0上 - Postgres JDBC thread stuck on java.net.SocketInputStream.socketRead0 even though timeout was defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM