問題タブ [java-threads]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票する
1 に答える
59 参照

java - スレッドから文字列を返す、またはスレッドから文字列を取得するにはどうすればよいですか?

スレッドから文字列値を取得する必要がありますが、取得できません! グローバル変数が機能しない! 助けてください。

これは私のコードです。私は使用する必要がありますdataString

0 投票する
0 に答える
92 参照

android - 待機せずにアップロード用の複数のスレッドを生成する方法

領収書テーブルから同期されていない領収書を取得してサーバーにアップロードする役割を担う次のクラスがあります。現在、次の関数はカーソルの結果セットを反復処理するだけです。

以下を使用して複数の非同期タスクを開始できることを認識しています。

上記のメソッドは、IntentService から呼び出す予定です。だからここに私の混乱があります:

1) do while ループは、次の反復のために asyncTask から制御が戻るまで待機しますか?

2) インテントサービス内で複数のスレッドを使用および生成すると、プログラムが中断されますか?

3)Runnable r = new Runnable()UI操作を意図していないので、AsyncTaskよりも使用したほうがよいですか?

0 投票する
4 に答える
3154 参照

java - 同期 (これ) は、同期ブロックのみをロックしますか、それともすべての「この」コードをロックしますか?

いくつかのスレッドを実行すると、そのうちのいくつかは静的関数getCount()を呼び出し、いくつかは新しいインスタンスを作成します。getCount()その時点で実際のインスタンス数を呼び出すたびに取得したいと考えています。

  1. コード内の 2 つのオプションに違いはありますか?
  2. " " をロックした場合、コンストラクターが同期ブロックを終了するまでthis呼び出すことができないということではありgetCount()ません (getCount() で同期を記述しない場合としましょう)。
  3. コード内のどこかで同期ブロックを実行すると、同期ブロックのみがロックされますか?それともすべての " this" コードがロックされますか?
  4. ここから編集: ありがとうございました。とても役に立ちましたが、回答に続いていくつか質問があります。
  5. 私が正しく理解していれば、同期された(this)ブロックは静的同期関数に影響を与えません(または接続されません)(numOfInstancesインクリメントではなくロック条件で)?
  6. インクリメントと getCount() 関数をスレッドセーフにするためのより良いオプションはありますか? (静的オブジェクトを開いて、synced(this) の代わりに synchronized(obj) を実行するように - 友人が提案)。
  7. ObjectCounter クラスに f1() メソッド (非静的) がある場合、1 つのスレッドが同期 (this) にあるときに、他のスレッドは f1() ブロック (同期クラスではないか、内部に同期ブロックがある) に入ることができますか?
  8. ObjectCounter に f1() メソッド (非静的) と f2() メソッド (非静的) がある場合、f1() に同期 (this) ブロックがあります。1 つのスレッドが synchronized(this) ブロックにある間、他のスレッドは f1() ブロックに入ることができますか (同期クラスではないか、内部に同期ブロックがあります)? (両方のスレッドが同じインスタンスで「動作」しているとしましょう)

`

0 投票する
2 に答える
660 参照

java - 30 秒後にスレッドを停止して再試行する

外部アプリケーションを呼び出している Restful Service があります。このアプリケーションにより、サービスがハングします。そのため、ユーザーが私のサービスを呼び出すと、この外部アプリケーションが原因で 1 時間かかる場合があります。外部アプリケーションの実行には数秒しかかかりません。そうでなければ、何かが間違っています。そのため、サービスのコードを最大 30 秒間実行したいと考えています。30秒を過ぎたらサービスを停止して再開したい。

これが私が望んでいるものです:

コードを 30 秒ごとに実行したくありません。コードの実行を停止して最初からやり直したい。

サービスは次のとおりです。

0 投票する
1 に答える
84 参照

java - このループのどこが間違っているのでしょうか?

私は現在、Java でテキストベースの RPG に取り組んでいます。プレイヤーが一定の歩数を歩くたびに、ランダムな戦闘が発生することになっています。戦闘システムを実行するたびに、プログラムが永久ループに入り、画面がフリーズします。

どうすればこれを修正できますか? これがすべて処理されるメイン クラスのコードです。

0 投票する
3 に答える
284 参照

android - ユーザー操作によるキャンバスへの描画は少し遅い

Androidでキャンバス画の勉強を始めたので、簡単なアプリを作りたいと思っています。

アプリの起動時に、いわゆる「ヘビ」が画面上で動き始めます。ユーザーが画面をタップすると、「ヘビ」が方向を変えます。

私はこれを簡単に達成しましたが、少し問題があります:

ユーザーが画面をタップすると、ヘビはその瞬間、数ミリ秒後に方向を変えることがあります。そのため、ユーザーはインタラクションが期待どおりに反応しないことを明確に感じることができます。たとえあなたが非常に集中していても、ヘビが向きを変える正確な瞬間はかなり予測できません. これを行うには、私が行った方法よりも優れた方法が他にあるはずです。

私のコードを確認してください。Runnable で Handler を使用してヘビを移動します。(キャンバスに描画し、それをビューの背景として設定するたびに、つまり setContentView をアクティビティに設定するたびに.

コード:

編集:

コードを変更しました。ビューにすべての詳細が含まれるようになり、フィリップが提案したようにonDrawメソッドと無効化メソッドを使用します。

結果は少し良くなりましたが、ユーザーの操作によって方向転換が遅れていることをはっきりと感じることができます。

おそらく私はスレッドで何かをすべきですか?

0 投票する
1 に答える
725 参照

java - Correct use of ProgressMonitorDialog's cancel button, interrupting threads, and showing progress

I've been using Java for a few years, but my thread knowledge is rubbish. I've Googled pretty heavily and found some good information about general use of ProgressMonitorDialog but nothing like my exact circumstances.

I'm currently using a ProgressMonitorDialog as a wrapper around an instance of IRunnableWithProgress, which in turn is a wrapper around a Thread. This works fine but now I'm trying to make the cancel button trigger an interrupt on the running thread, which I can handle to gracefully terminate the operation.

One important thing to note is that I have two plugins; "Data" and "UI". The data plugin contains all of the real work, and must be independent from the UI or any Eclipse plugins. The UI plugin should be as thin as possible.

Here's a distilled version of the code I've got so far.

Data:

UI:

So when the cancel button is clicked, myThread.interrupt() is called. Now the thread needs to respond to the interrupt. Data.createThread() now looks something like this:

It might be rather verbose polling the interrupted state like this, but I can't see this causing any problems.

But, what if Thing.operationA() wasn't atomic, and could be interrupted within that function:

How would I detect and handle a change to the interrupted state between atomic1() and atomic2()? Is it as simple as polling Thread.currentThread.isInterrupted() again? Or will I need to pass around some volatile object to track the interrupted state? Should I be throwing InterruptedException somewhere?

My second question is about tracking and reporting progress. I understand how IProgressMonitor.worked() should be used. As already seen, my Data thread contains 3 operations. Is it possible to pass that information up to the UI so I can track the progress in the ProgressMonitorDialog?

Ideally, something like this:

However as stated, Data cannot depend on Eclipse and therefore passing the IProgressMonitor doesn't work in this case.

Could I have a variable tracking progress in my thread, and then call something like myThread.getProgress() asynchronously from the UI thread to update the progress bar with new work? I'm not sure how feasible this is (it popped into my head as I was writing this question) so I'll try that next.

Lots of information and question marks in here, sorry if my style is a bit scattered. I could elaborate more if needs be but this is already a wall of text. Any information, advice or ideas appreciated.