我可以将while循环与wait()和notify()/ notifyall()一起使用吗?
编辑:我希望waitfornextprice方法的else条件只对putPrice方法执行.
public synchronized void putPrice(String s,int i) {
if (getPrice(s) != i){
this.Prices.put(s, i);
notify();
}
}
public synchronized int waitForNextPrice(String s) {
int b = null;
if (hasPriceChanged(s)==true){
b = getPrice(s);
}
else{
//Need to block here, until putPrice() is called again
while(hasPriceChanged(s)==false) wait();
//Once putPrice has been called, b = getPrice();
b = getPrice(s);
}
return b;
}
Run Code Online (Sandbox Code Playgroud) 我想知道你们是否可以帮助我。我正在尝试使用 Java 的内置图形模块制作一个动画程序......问题是,Java 一次执行所有内容;不同的动画之间没有任何时间。最终产品只是最后一张照片。我需要一个在每张图片之间放置半秒的函数。
任何帮助表示赞赏。
规格:Blue-J,JDK 6。
编辑:顺便说一句,我是一个 Java 新手,这是一个类的东西。任务是制作动画,然后按“c”前进每一帧,但我认为那有点像贫民窟,所以我想要更好的东西。
我有一个场景,我有一个线程在等待和执行任务之间循环.但是,我想中断线程的等待(如果你愿意的话,跳过剩下的等待)并继续执行任务.
任何人都有任何想法如何做到这一点?
我怎样才能做到这一点 ?我尝试了一些线程但没有发生任何事情.
myView.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
myButton.setClickable(false);
//Here i want to wait 1 second then:
myButton.setClickable(true);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
请问有人在我的代码中等待一秒钟吗?
在我的应用程序的某个时刻,我想让我的主线程(即当前正在执行的线程)休眠一段时间或直到后台完成(并唤醒它),以先到者为准。
这是我所做的(我认为会起作用,但没有)
public static void main(String args[])
{
// .... other stuff ...
// show a splash screen
// just think of this as an image
showPlashScreen():
new Thread(new Runnable()
{
public void run()
{
// do some work here
// notify everyone after the work is done
Thread.currentThread().notifyAll();
}
}).start();
// now make the current thread to wait for the background
// or for at most 1000
Thread.currentThread().wait(1000);
disposeSplashScreen();
// ... other stuff ....
}
Run Code Online (Sandbox Code Playgroud)
执行这个,我不断得到 java.lang.IllegalMonitorStateException
(部分)堆栈跟踪:
Caused …Run Code Online (Sandbox Code Playgroud) I'm making a news board that fades the news in and out. When the element fades out the content gets deleted, when it fades back in there is new content in its place. This works, just the timing is off. The new content that should only appear when the old content has faded away, appears as the element is fading. Is there a way to wait until this is done?
$(document).ready(function() {
var counter = 0;
var slides = ['<p>Track …Run Code Online (Sandbox Code Playgroud) 我正在使用多线程来解决经典的生产者 - 消费者问题.我正在使用wait()和notifyAll()在代码中.我的问题是,当notifyAll通知其他等待线程恢复时,它不会立即恢复.这是为什么?代码如下
public class ConsumerProducer {
private int count;
public synchronized void consume() {
while (count == 0) { // keep waiting if nothing is produced to consume
try {
wait(); // give up lock and wait
} catch (InterruptedException e) {
// keep trying
}
}
count--; // consume
System.out.println(Thread.currentThread().getName() + " after consuming " + count);
}
public synchronized void produce() {
count++; //produce
System.out.println(Thread.currentThread().getName() + " after producing " + …Run Code Online (Sandbox Code Playgroud) using System;
using System.Threading.Tasks;
namespace _1._8_Starting_A_New_Task
{
public static class Program
{
public static void Main()
{
Task t = new Task(() =>
{
for (int x = 0; x < 100; x++)
{
Console.Write('*');
}
});
t.Wait();
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我对从头开始编程还很陌生,到目前为止主要使用了几年统一,所以我的一般编程知识非常好。暑假后开始在大学学习游戏开发,但我们从头开始编程,作为一项任务,我们必须在课堂上共同制作的 2D 引擎中制作一个简单的游戏。
所以我决定制作的游戏是炸弹人的副本,并且我已经达到了我现在使炸弹起作用的程度。
我遇到的问题是我不知道如何正确添加一个计时器来倒计时炸弹爆炸的时间,以便玩家可以避免它。
我已经尝试过 SDL_Delay 和 _sleep,它们都只是暂停整个程序,因此不起作用,我已经四处寻找更多选项,但并没有真正理解事情是如何工作的。如果我能得到一些示例和页面链接来解释如何正确地制作这样的东西(一些简单而小巧的东西:P),那么我将不胜感激!
请注意,我们在引擎中使用 SDL。
谢谢!
我是 Java 新手。我最近在学校学习多线程。我尝试创建一个小程序,该程序可以将任务分成更小的部分,并使用循环在单独的线程中运行每个部分。问题是在循环之后我需要对结果求和并将其打印出来,但是循环之后的打印在线程完成之前运行。
我所有的同学都sleep在打印结果之前添加,但是当线程花费太长时间时这不起作用。
有没有办法在运行其他代码之前等待循环中的所有线程先完成?
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
public class threatGenerator {
static int count = 0;
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.print("Input start: ");
int start = Integer.parseInt(sc.nextLine());
System.out.print("Input end: ");
int end = Integer.parseInt(sc.nextLine());
int threadNumber = (int)Math.ceil((end - start)/100.0) ;
System.out.println("Running "+threadNumber+" threads.");
for(int i = 0 ;i < threadNumber;i++){
int temp = start+ 99;
if(temp>end) temp = end;
String name = String.valueOf(i);
ThreadDemo …Run Code Online (Sandbox Code Playgroud)