回答編集履歴

6

コード修正

2022/12/04 04:21

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -36,7 +36,7 @@
36
36
  @Override
37
37
  public void onClick(View v) {
38
38
  int c = count ++;
39
- log("push Button(" + count + ").");
39
+ log("push Button(" + c + ").");
40
40
 
41
41
  lastExecutor.request(() -> {
42
42
  log("process(" + c + ") start.");

5

java を kotlin に合わせて修正

2022/12/04 04:19

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -23,22 +23,22 @@
23
23
  super.onCreate(savedInstanceState);
24
24
  setContentView(R.layout.activity_main);
25
25
 
26
- LastExecutor lastExecutor = new LastExecutor();
27
- Random random = new Random();
28
-
29
26
  handler = new Handler(Looper.getMainLooper());
30
27
  textView = findViewById(R.id.textView);
31
28
  scrollView = findViewById(R.id.scrollView);
32
29
 
30
+ LastExecutor lastExecutor = new LastExecutor();
31
+
33
32
  Button button = findViewById(R.id.requestButton);
34
33
  button.setOnClickListener(new View.OnClickListener() {
34
+ Random random = new Random();
35
35
  int count = 0;
36
36
  @Override
37
37
  public void onClick(View v) {
38
- count ++;
38
+ int c = count ++;
39
39
  log("push Button(" + count + ").");
40
+
40
41
  lastExecutor.request(() -> {
41
- int c = count;
42
42
  log("process(" + c + ") start.");
43
43
  try {
44
44
  Thread.sleep(random.nextInt(3000) + 2000); //2~5秒

4

kotlin コード追加

2022/12/04 04:13

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -125,3 +125,96 @@
125
125
  </androidx.constraintlayout.widget.ConstraintLayout>
126
126
  ```
127
127
  ![実行してボタンを適当に押した後のスクリーンショット](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-04/a3b56b41-30bc-455c-9648-0d0869f278ca.png)
128
+
129
+ ---
130
+ 私も AndroidStudio に kotlin 変換してもらって修正したらこんな感じになりました。
131
+ こちらはそれこそ参考ということで…。
132
+
133
+ MainActivity.kt
134
+ ```kotlin
135
+ import android.os.Bundle
136
+ import android.os.Handler
137
+ import android.os.Looper
138
+ import android.view.View
139
+ import android.widget.Button
140
+ import android.widget.ScrollView
141
+ import android.widget.TextView
142
+ import androidx.appcompat.app.AppCompatActivity
143
+ import java.time.LocalTime
144
+ import java.util.*
145
+
146
+ class MainActivity : AppCompatActivity() {
147
+ private lateinit var handler: Handler
148
+ private lateinit var textView: TextView
149
+ private lateinit var scrollView: ScrollView
150
+
151
+ override fun onCreate(savedInstanceState: Bundle?) {
152
+ super.onCreate(savedInstanceState)
153
+ setContentView(R.layout.activity_main)
154
+
155
+ handler = Handler(Looper.getMainLooper())
156
+ textView = findViewById(R.id.textView)
157
+ scrollView = findViewById(R.id.scrollView)
158
+
159
+ val lastExecutor = LastExecutor()
160
+
161
+ val button: Button = findViewById(R.id.requestButton)
162
+ button.setOnClickListener(object : View.OnClickListener {
163
+ val random = Random()
164
+ var count = 0
165
+ override fun onClick(v: View) {
166
+ val c = count++
167
+ log("push Button($c).")
168
+
169
+ lastExecutor.request {
170
+ log("process($c) start.")
171
+ try {
172
+ Thread.sleep((random.nextInt(3000) + 2000).toLong()) //2~5秒
173
+ } catch (_: InterruptedException) {
174
+ }
175
+ log("process($c) end.")
176
+ }
177
+ }
178
+ })
179
+ }
180
+ private fun log(text: String) {
181
+ handler.post {
182
+ textView.append("${LocalTime.now()}: $text\n")
183
+ scrollView.fullScroll(ScrollView.FOCUS_DOWN)
184
+ }
185
+ }
186
+ }
187
+
188
+ class LastExecutor {
189
+ private var queue: Runnable? = null
190
+ private var worker: Thread? = null
191
+ private val lock = Object()
192
+
193
+ fun request(process: Runnable?) {
194
+ synchronized(lock) {
195
+ queue = process
196
+ lock.notifyAll()
197
+ }
198
+ if (worker == null) startWorker()
199
+ }
200
+
201
+ private fun startWorker() {
202
+ worker = Thread {
203
+ try {
204
+ while (true) {
205
+ var p: Runnable
206
+ synchronized(lock) {
207
+ while (queue == null) lock.wait()
208
+ p = queue as Runnable
209
+ queue = null
210
+ }
211
+ p.run()
212
+ }
213
+ } catch (e: InterruptedException) {
214
+ e.printStackTrace()
215
+ }
216
+ }
217
+ worker!!.start()
218
+ }
219
+ }
220
+ ```

3

コードの変数名が頭大文字だったのを小文字化

2022/12/04 03:51

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -23,7 +23,7 @@
23
23
  super.onCreate(savedInstanceState);
24
24
  setContentView(R.layout.activity_main);
25
25
 
26
- LastExecutor LastExecutor = new LastExecutor();
26
+ LastExecutor lastExecutor = new LastExecutor();
27
27
  Random random = new Random();
28
28
 
29
29
  handler = new Handler(Looper.getMainLooper());
@@ -37,7 +37,7 @@
37
37
  public void onClick(View v) {
38
38
  count ++;
39
39
  log("push Button(" + count + ").");
40
- LastExecutor.request(() -> {
40
+ lastExecutor.request(() -> {
41
41
  int c = count;
42
42
  log("process(" + c + ") start.");
43
43
  try {

2

レイアウト貼り忘れ

2022/12/04 03:21

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -1,6 +1,8 @@
1
1
  停止も無いやっつけでしかも java で申し訳ないですが、こんな感じは如何でしょう。
2
2
  勉強中ということで基本と思われる wait - notify です。
3
3
  process が終わるまでに何度ボタンを押しても、最後のが次に実行されます。
4
+
5
+ MainActivity.java
4
6
  ```java
5
7
  import androidx.appcompat.app.AppCompatActivity;
6
8
 
@@ -87,4 +89,39 @@
87
89
  }
88
90
  }
89
91
  ```
92
+ res/layout/activity_main.xml
93
+ ```xml
94
+ <?xml version="1.0" encoding="utf-8"?>
95
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
96
+ xmlns:app="http://schemas.android.com/apk/res-auto"
97
+ xmlns:tools="http://schemas.android.com/tools"
98
+ android:layout_width="match_parent"
99
+ android:layout_height="match_parent"
100
+ tools:context=".MainActivity">
101
+
102
+ <Button
103
+ android:id="@+id/requestButton"
104
+ android:layout_width="wrap_content"
105
+ android:layout_height="wrap_content"
106
+ android:text="request"
107
+ app:layout_constraintEnd_toEndOf="parent"
108
+ app:layout_constraintStart_toStartOf="parent"
109
+ app:layout_constraintTop_toTopOf="parent" />
110
+ <ScrollView
111
+ android:id="@+id/scrollView"
112
+ android:layout_width="0dp"
113
+ android:layout_height="0dp"
114
+ app:layout_constraintBottom_toBottomOf="parent"
115
+ app:layout_constraintEnd_toEndOf="parent"
116
+ app:layout_constraintStart_toStartOf="parent"
117
+ app:layout_constraintTop_toBottomOf="@id/requestButton">
118
+ <TextView
119
+ android:id="@+id/textView"
120
+ android:layout_width="match_parent"
121
+ android:layout_height="wrap_content"
122
+ android:textAlignment="textStart"
123
+ android:textSize="20dp" />
124
+ </ScrollView>
125
+ </androidx.constraintlayout.widget.ConstraintLayout>
126
+ ```
90
127
  ![実行してボタンを適当に押した後のスクリーンショット](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-04/a3b56b41-30bc-455c-9648-0d0869f278ca.png)

1

説明修正

2022/12/03 18:48

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -1,5 +1,5 @@
1
1
  停止も無いやっつけでしかも java で申し訳ないですが、こんな感じは如何でしょう。
2
- 勉強中ということで wait - notify による排他制御です。
2
+ 勉強中ということで基本と思われる wait - notify です。
3
3
  process が終わるまでに何度ボタンを押しても、最後のが次に実行されます。
4
4
  ```java
5
5
  import androidx.appcompat.app.AppCompatActivity;