4 Threads can log by pressing the relevant button

This commit is contained in:
Paul 2020-11-09 10:21:34 +01:00
parent f0ff3b2a04
commit b814476d30
3 changed files with 67 additions and 10 deletions

View File

@ -4,17 +4,37 @@ import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
TestThread[] threadArray = new TestThread[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestThread testThread = new TestThread();
testThread.start();
for(int i=0; i<threadArray.length;i++){
threadArray[i]=new TestThread();
threadArray[i].setName("Thread_"+i);
threadArray[i].start();
}
Log.v("test","test");
//connect Buttons to Threads
Button button1 = findViewById(R.id.Thread1_btn);
Button button2 = findViewById(R.id.Thread2_btn);
Button button3 = findViewById(R.id.Thread3_btn);
Button button4 = findViewById(R.id.Thread4_btn);
button1.setOnClickListener(v ->
Log.v("Thread_1", String.valueOf(threadArray[0].getCounter())));
button2.setOnClickListener(v ->
Log.v("Thread_2", String.valueOf(threadArray[1].getCounter())));
button3.setOnClickListener(v ->
Log.v("Thread_3", String.valueOf(threadArray[2].getCounter())));
button4.setOnClickListener(v ->
Log.v("Thread_4", String.valueOf(threadArray[3].getCounter())));
}
}

View File

@ -1,12 +1,12 @@
package com.ploedige.threadtest;
import android.util.Log;
public class TestThread extends Thread{
private int counter;
public void run() {
while (true){
for(int i =1; i<=20; i++){
Log.v("TestThread", String.valueOf(i));
counter = i;
try {
this.sleep(250);
} catch (InterruptedException e) {
@ -15,4 +15,6 @@ public class TestThread extends Thread{
}
}
}
public int getCounter(){return counter;}
}

View File

@ -6,13 +6,48 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
<Button
android:id="@+id/Thread1_btn"
android:text="Thread 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/Thread2_btn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/Thread2_btn"
android:text="Thread 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/Thread1_btn"
app:layout_constraintBottom_toTopOf="@id/Thread3_btn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/Thread3_btn"
android:text="Thread 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/Thread2_btn"
app:layout_constraintBottom_toTopOf="@id/Thread4_btn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/Thread4_btn"
android:text="Thread 4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/Thread3_btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>