假设我有两个线程A和B,它们都在运行。突然,B决定终止A,因此A应该尽快终止。我不希望A立即终止,而是在它达到稳定状态时终止。
一个简单的解决方案是使用共享变量terminatePlease,A会在每次达到稳定状态时检查该变量。
代码的原型如下所示:
class A extends Thread {
boolean terminatePlease;
public void run() {
while (true) {
if (terminatePlease)
return;
doU
我使用线程在后台运行一个计算开销很大的代码。这段代码在他运行时返回进度。在主线程中,我想读取这个值,这样我就可以向用户显示进度
下面是我的设置:一个process类:
Public class ProcessThread implements Runnable {
public int progress;
ProcessThread{
}
public void run(){
// Update progress as the processing goes
}
}
和一个主类:
Public class MainClass {
我不明白为什么在这个实现中stopped不是volatile --如果一个不同的线程更新了它,它会被正确地反映出来吗?
第二,测试(!Stopping)是原子的吗?
using System;
using System.Threading;
/// <summary>
/// Skeleton for a worker thread. Another thread would typically set up
/// an instance with some work to do, and invoke the Run method (eg with
/// new Threa
我有一条线
write a = 0
write a = 1
write volatile flag = 1
在第二条线上
read volatile flag // This always happens after I write volatile flag in thread 1
read a
是否可以进行重新排序,以便在第二个线程中看到read a返回0?
如果没有,能请人详细解释原因吗?
我之所以问这个问题,是因为我对JLS的定义感到困惑:
在每个线程t执行的所有线程间操作中,t的程序顺序是一个总顺序,它反映了根据t的线程内语义执行这些操作的顺序。
在这种情况下,似乎允许重新排
下面的代码是线程安全的吗?尽管一个线程只有一次写入和多次读取(使用while循环,如下所示),但我真的需要将dataReady设置为volatile吗?
public class MyApplication extends Application
{
/* a flag indicates if the data is fully retrieved from the database */
private volatile boolean dataReady = false;
@Override
public void onCreate()
{
我定义了一个可与某些线程共享的公共易失性LinkedList,并在main()中同步它,这是静态的.因此,我得到一个编译错误:“无法引用非静态字段”。
如果我将该定义修改为公共易失性静态LinkedList,则会得到一个异常:
Exception in thread "filteringThread" java.lang.NullPointerException
at com.swimtechtest.swimmerbench.FilteringThread.run(FilteringThread.java:39)
这是我代码的一部分
public class Swimmer
我试图在一个无限的while循环中运行一个for循环。当没有print语句时,代码不会按预期运行,但当有print语句时,它运行得很好。代码如下: class Test implements Runnable{
public static int varint = 0;
public static void main(String args[]){
Thread x = new Thread(new Test());
int i;
x.start();
while(true){
System.out.println("Hello W
public class Stuff {
private final Timer timer = new Timer(true);
public static final int DEFAULT_TIMEOUT = 1500;
private volatile int timeout = DEFAULT_TIMEOUT;
public void doStuff(OtherStuff) {
...
timer.schedule(timeout, ...);
}
public void setTimeout(int timeout) {
因此,我有一个负责所有其他线程的MainThread类(创建、停止、监视)。
public class MainThread implements Runnable {
public static volatile boolean keepRunning = true;
public void run(){
//some code..
while(keepRunning){
//here i am creating threads for my Wired class (explaining below)
我目前正在开发一个Java战斗游戏,但我遇到了障碍。
基本上,我有两个线程在运行:一个线程由画布实现,它更新绘制到屏幕上的所有内容,然后睡觉;另一个线程由字符类实现,它只更新字符的位置。我在画布类中还有一个子类,它实现KeyListener,并为键的状态更改一个布尔变量,如果按了up按钮,那么字符自己的up布尔变量也会被更新。
我的问题是,当我按下键盘上的一个按钮时,输入肯定是在画布一侧进行的(我已经用打印语句确认了输入),但它并不总是指向字符,我只能假设,由于字符的位置更新在一个单独的线程中运行,所以出现了一个问题。
这是我的相关代码:
//
public class GameWindow
我在查看System.Threading.Tasks (.NET标准2.0)中任务的一些实现细节时,偶然发现了这段有趣的代码:
internal volatile int m_stateFlags;
...
public bool IsCompleted
{
get
{
int stateFlags = m_stateFlags; // enable inlining of IsCompletedMethod by "cast"ing away the volatiliy
return IsCompletedMethod(stateFlags);
我对Java多线程很陌生,并试图创建一个有2个线程的程序。一个打印奇数,一个打印偶数。现在,我要打印从1到1000的所有数字,这样每一个线程都会转一圈。我已经创建了一个转身对象,它是由两个对象共享的,因此可以用来向另一个线程发送信号!下面给出了代码
public class Turn {
public static int whoseTurn = 1;
public static int getWhoseTurn() {
return whoseTurn;
}
}
public class Main {
pu
我有一个工人Thread,它有一个与它相关的Handler。Handler接收Message,并在worker Thread中执行一些操作。我想“停止”Thread in onPause() of my Activity。在我的Activity的Activity方法中,我向我的Handler发送一条退出消息,这样我的工作人员Thread就可以脱离Looper.loop()并从run()返回。
这是阻止Thread的正确方法吗?若否,应如何进行?
我看到了从主Thread调用t1的代码示例,其中t1是他们的worker Thread。
join()的目的是什么?
在我的示例中需要执
我有以下代码,这些代码被初始化为类中的静态变量:
public class MyXlet extends Xlet {
boolean connected = false;
...
void connect() {
// some code goes here, starts a new thread
MyXlet.connected = true;
}
void disconnect() {
// some code goes here, the new thread is designed to terminate on
考虑到下面的玩具示例类:
public class Test {
private volatile Outer outerVar = new Outer();
static class Outer {
Inner innerVar = new Inner();
}
static class Inner {
// state
// setters
// getters
}
private void multithreadedUse() {
// play
我知道堆栈溢出中有很多可用的主题,但是当我试图从理论上了解易失性关键字时,我相信它对我来说很清楚,但是当我尝试运行下面的示例时,我的理解失败了,或者我无法可视化这个序列。
对挥发性->Making variable as volatile for an instance, makes it non cacheable for threads that means whichever thread is accessing the volatile variable it has to flush the changes into the main memory immediately s
在下面的例子中
private Integer a = 10;
private Integer c = -10;
private int b = -10;
public void acquire(){
synchronized(a){
print("acquire()");
try{
b = -12;
//Thread.sleep(5000);
a
这里我有三个简单的类:
第一类:
public class ThreadSyncMain {
public static int count = 0; // volatile is not use
public static void main(String[] args) {
Thread thread1 = new Thread( new Thread1(),"Thread1" );
Thread thread2 = new Thread( new Thread2(),"Thread2");
thread1.start();
我是Java8和多线程工作的新手。我试过下面这段代码
public class Test {
public static boolean bchanged = true;
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
while (true) {
if (bchanged != bchanged) {
我正在尝试使用通知()和等待()。这是我想要的课程。当我试图给addNewItem()打电话时,我遇到了一个问题。如果我先调用tryToReadItem(),然后调用addNewItem()方法,则不会打印该日志。请注意,我的DemoClass是单例。
public class DemoClass {
private static final String TAG = "DemoClass";
private static DemoClass instance;
private Object lock = new Object();
private static Threa
我正在尝试创建一个示例来显示易失性变量和普通变量之间的差异,例如:
package main;
public class TestVolatile extends Thread {
public int l = 5;
public volatile int m = -1;
public TestVolatile(String str) {
super(str);
}
public void run() {
int i = 0;
while ((l > 1) && (l <
这个问题已经在两篇博客文章(,)中讨论过了,但我还没有听到一个明确的答案。如果我们有一个线程可以做到这一点:
public class HeartBeatThread extends Thread {
public static int counter = 0;
public static volatile int cacheFlush = 0;
public HeartBeatThread() {
setDaemon(true);
}
static {
new HeartBeatThread().start();
}
public void