如何在Android中运行2个线程?

时间:2019-07-01 06:15:55

标签: java android multithreading networking

我当前正在为一个应用程序工作,我希望应用程序在用户单击按钮时运行其中一个线程,而另一个在用户启动应用程序时运行。但是此代码的问题是,如果第二个线程当前正在运行,则第一个线程未运行。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="16dp">


    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Message"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Send"
        android:id="@+id/button1"/>


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class MainActivity extends AppCompatActivity {


public static String messagee;

EditText et;

TextView tw;

Button bt1, bt2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (EditText) findViewById(R.id.editText1);

    messagee = et.getText().toString();

    tw = (TextView) findViewById(R.id.textView);
    bt2 = (Button) findViewById(R.id.button2);
    bt1 = (Button) findViewById(R.id.button1);


    final ExecutorService executorService = Executors.newFixedThreadPool(2);


    final Thread t1 = new Thread(new threadOne());

    Thread t2 = new Thread(new threadTwo());

    executorService.execute(t2);

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            executorService.execute(t1);
        }
    });

}

public class threadOne implements Runnable {

    private String message;

    @Override
    public void run() {

        try {

            this.message = MainActivity.messagee;

            byte[] buf = message.getBytes();

            DatagramSocket udpSocket = new DatagramSocket(5001);

            InetAddress serverAddr = InetAddress.getByName("10.1.128.22");

            DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, 59948);

            udpSocket.send(packet);

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}

public class threadTwo implements Runnable {

    @Override
    public void run() {

        String text;

        try {
            byte[] buf2 = new byte[1024];

            DatagramSocket ds = new DatagramSocket(5001);

            DatagramPacket dp = new DatagramPacket(buf2, buf2.length);

            ds.receive(dp);

            text = new String(dp.getData());
            tw.setText(text);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

}

0 个答案:

没有答案