我正在尝试终止一个线程,但它不会中断或停止。所有这些都是名为 Webots 的软件的控制器的一部分。我用它来模拟多机器人系统。在每个机器人的控制器中,我启动一个线程,通过机器人接收器接收消息。该线程必须首先启动,并在模拟结束时终止。
该线程的 run 方法如下所示:
public void run() {
while (true)
{
String M = recieveMessage();
char[] chars = M.toCharArray();
if(chars[0]==robotName||chars[0]=='0')
messages.add(M);
}
}
Run Code Online (Sandbox Code Playgroud)
在主控制器中,我的代码如下所示:
MessageThread MT = new MessageThread(messages, receiver,getName());
MT.start();
for (int i = 0; i < 100; i++)
{
try
{
Thread.sleep(25); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println(messages.get(messages.size()-1));
}
MT.interrupt();//MT = null;
System.out.println(MT.interrupted());
Run Code Online (Sandbox Code Playgroud)
我在主控制器中做什么并不重要,所以不要评判它。例如,消息是一个 ArrayList。它就像一个缓冲区,MT 将消息放入其中,主线程从中读取。我使用它是因为接收器和发射器不同步。
如果我调用interrupt() 或MT = null 但interrupted() 它返回false 并且MT 继续运行。我的代码有什么问题吗?
我阅读了一些主题,例如:
http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
等等,但我找不到任何有用的答案。
谢谢大家,我已经对我的代码进行了更改。我将此添加到 …
我正在尝试与 Java 聊天。一切正常,除了特殊字符不起作用。我认为这是一个编码问题,因为我Outputstream在 UTF-8 中对字符串进行编码,如下所示:
protected void send(String msg) {
try {
msg+="\r\n";
OutputStream outStream = socket.getOutputStream();
outStream.write(msg.getBytes("UTF-8"));
System.out.println(msg.getBytes("UTF-8"));
outStream.flush();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但在我的receive方法中,我没有找到一种方法来做到这一点:
public String receive() throws IOException {
String line = "";
InputStream inStream = socket.getInputStream();
int read = inStream.read();
while (read!=10 && read > -1) {
line+=String.valueOf((char)read);
read = inStream.read();
}
if (read==-1) return null;
line+=String.valueOf((char)read);
return line;
}
Run Code Online (Sandbox Code Playgroud)
那么有没有一种快速的方法来指定缓冲区读取的字节是用 UTF-8 编码的?
编辑:好的,我试过BufferedReader这样的:
public String receive() throws …Run Code Online (Sandbox Code Playgroud) 我正在尝试了解其get_rect()工作原理。在这个简单的示例中,我有两个图像,并且想要获取第二个图像的位置并将第一个图像移动到第二个图像。
我在网上查看了各种各样的示例,但无法正常工作。我究竟做错了什么?
import pygame, sys
from pygame.locals import *
import time
pygame.init()
FPS = 10 # frames per second setting
fpsClock = pygame.time.Clock()
# Set up the window
DISPLAYSURF = pygame.display.set_mode((600, 400), 0, 32)
pygame.display.set_caption('Test program for get_rect()')
WHITE = (255, 255, 255)
# Load two images
baseImg = pygame.image.load('image1.jpg')
spaceshipImg = pygame.image.load('image2.jpg')
DISPLAYSURF.fill(WHITE)
# Place one image at the bottom of the screen
DISPLAYSURF.blit(baseImg, (300, 300))
pygame.display.update()
# Place the second image at the top of the …Run Code Online (Sandbox Code Playgroud) 我试图在 Pygame 脚本中使图像的背景透明。现在我的游戏背景是黑色的,而不是透明的。我在其他地方阅读了我可以使用的内容convert_alpha,但它似乎不起作用。
这是我的代码(的相关部分):
import PIL
gameDisplay = pygame.display.set_mode((display_width, display_height))
img = pygame.image.load('snakehead1.bmp').convert_alpha(gameDisplay)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?提前致谢!
我正在寻找一种方法来查看 apygame.mixer.Channel当前是否正在播放声音。例如,仅在特定通道中播放的声音结束后才执行某些操作。这是我当前播放声音的代码:
import pygame
pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()
pygame.mixer.Channel(0).play(pygame.mixer.Sound('coolsound.wav'), maxtime=2000)
Run Code Online (Sandbox Code Playgroud)
我正在考虑这样的 if 语句:
if pygame.mixer.Channel(0) = playing a sound:
print("playing a sound")
else:
print("not playing")
Run Code Online (Sandbox Code Playgroud)
显然这是行不通的,只是为了让你了解我在寻找什么。谢谢!
我有一个函数需要一个两个元素的整数序列。我可以通过这样做来为元组创建类型提示,typing.Tuple[int, int]当我尝试传递更少或更多的值时,Pycharm 会警告我。但是,它不适用于typing.Sequence.
如何为所有两个元素序列创建类型提示?
我正在尝试从具有std::stringas键的无序映射中接收值,其中一些字符串只包含一个字符.我的所有输入都来自于std::stringstream我从中获取每个值并将其转换为char,然后将其转换为字符串使用
std::string result {1, character};,根据文档和此答案,这似乎是有效的.
但是,当我这样做时,字符串前置\ x01(对应于值1).这使得在地图中找不到字符串.我的调试器还确认字符串大小为2,值为"\ x01H".
为什么会发生这种情况,我该如何解决?
#include <iostream>
#include <sstream>
#include <unordered_map>
int main()
{
const std::unordered_map<std::string, int> map = { {"H", 1}, {"BX", 2} };
std::stringstream temp {"Hello world!"};
char character = static_cast<char>(temp.get()); // character is 'H'
std::string result {1, character}; // string contains "\x01H"
std::cout << result << " has length " << result.size() << std::endl; // H has length 2
std::cout << map.at(result) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我已经开始使用 c++(来自 ac# 背景),并且在我的程序中我需要运行一个 while true 循环,但是由于它是一个 imgui 程序,ui 完全冻结,因为我Sleep()在循环中使用。我需要创建一个新线程,但我在网上找到的所有内容都只是
std::thread nThread(Method);
nThread.join();
Run Code Online (Sandbox Code Playgroud)
现在,问题在于它根本不起作用,因为我假设这是一个始终在运行的 while 循环。我想在 C# 中做 C++ 等效的Thread thread = new Thread(method)和thread.Start();。如果有人可以帮助我,我将不胜感激。
我在实际代码中遇到问题,并使用以下示例代码复制了该问题。
\n#include <iostream>\n#include <tuple>\n\nusing namespace std;\n\nstruct Identity\n{\n template <typename... T>\n static std::tuple<T...> Apply(T... val)\n {\n return std::tuple(val...);\n }\n};\n\ntemplate <typename F, typename... T>\nstd::tuple<T...> Apply(T... t)\n{\n return F::Apply<T...>(t...);\n}\n\nint main()\n{\n const auto t = Apply<Identity>(1., 2., 3.);\n cout << std::get<0>(t);\n cout << std::get<1>(t);\n cout << std::get<2>(t);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n编译错误:
\nmain.cpp:26:22: error: expected primary-expression before \xe2\x80\x98...\xe2\x80\x99 token\n return F::Apply<T...>(t...);\n ^~~\nmain.cpp:26:22: error: expected \xe2\x80\x98;\xe2\x80\x99 before \xe2\x80\x98...\xe2\x80\x99 token\nmain.cpp:26:22: error: expected primary-expression before \xe2\x80\x98...\xe2\x80\x99 token\nRun Code Online (Sandbox Code Playgroud)\n如果我从有问题的语句中删除 <T...> return F::Apply(t...);,并让编译器推断出类型,它就会起作用。但是,在我的现实世界代码中,我需要指定类型。特定类型并满足编译器的正确语法糖是什么?
我有一个清单
arr = [0, 1, 45, 2, 40, 3, 70, 4, 45, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
其中我试图使用以下代码从 3 个连续元素中找到最大元素的位置/索引:
for i in range (0, len(arr)-3):
print(arr.index(max(arr[i : i+3])))
Run Code Online (Sandbox Code Playgroud)
当i转到位置 7 时,它给出了错误的结果。
结果应该是:
2 2 2 4 6 6 6 8 8 11 12
而是相反
2 2 2 4 6 6 6 2 2 11 12
(据我所知)C++ 不接受头文件上的“.h”扩展名(因为它通常不存在于其他包含语句中)所以包含如何<bits/stdc++.h>在 C++ 中工作以及为什么它有“.h”延期?
表达式:header->_block_use == block_use || 标头->_block_use == _CRT_BLOCK && block_use == _NORMAL_BLOCK
这是什么意思?它与标题有关吗?
我在我的代码中发现,在我跳过它之后返回调试断言失败,只有这一行代码在其他项目中有效。
是否有我必须删除的链接器设置或 c/c++ 设置?
整型变量是 4 字节或 32 位,二进制数中的 2^31 和 -2^31 都是 32 位。但是,当您将 2^31 = 2,147,483,648 放入整数变量时,它会显示错误,但对于 -2^31 则没问题。为什么?
c++ ×6
python ×4
java ×3
pygame ×3
c++17 ×2
audio ×1
encoding ×1
for-loop ×1
header-files ×1
if-statement ×1
list ×1
mixer ×1
pycharm ×1
python-3.5 ×1
python-3.x ×1
sockets ×1
sstream ×1
std ×1
string ×1
type-hinting ×1
utf-8 ×1