我通过以下方式创建了一个线程服务:
public class TCPClientService extends Service{
...
@Override
public void onCreate() {
...
Measurements = new LinkedList<String>();
enableDataSending();
}
@Override
public IBinder onBind(Intent intent) {
//TODO: Replace with service binding implementation
return null;
}
@Override
public void onLowMemory() {
Measurements.clear();
super.onLowMemory();
}
@Override
public void onDestroy() {
Measurements.clear();
super.onDestroy();
try {
SendDataThread.stop();
} catch(Exception e){
...
}
}
private Runnable backgrounSendData = new Runnable() {
public void run() {
doSendData();
}
};
private void enableDataSending() {
SendDataThread = new Thread(null, …Run Code Online (Sandbox Code Playgroud) 是否有可能为Executors执行的任务设置优先级?我在JCIP中发现了一些关于它可能的陈述,但我找不到任何例子,我找不到任何与文档相关的内容.
来自JCIP:
执行策略指定任务执行的"内容,位置,时间和方式",包括:
- ...
- 应该以什么顺序执行任务(FIFO,LIFO,优先顺序)?
- ...
UPD:我意识到我不确切地问我想问什么.我真正想要的是:
如何使用/模拟thread.setPriority()执行程序框架设置线程优先级(即什么)?
Console.WriteLine在输出写入或立即返回之前是否阻塞?
如果它确实阻塞了是否有一种向控制台写入异步输出的方法?
这里有一个问题,我不知道如何修复.我正在做一个涉及GUI和串行数据的小项目.GUI由主线程运行,并且由于保存我的传入串行数据的数据变量需要不断更新,因此这些变量将在第二个线程中更新.问题是当我需要更新GUI上的一些文本框时,需要使用辅助线程中的数据更新这些文本框,这就是我的问题所在.我不能直接从辅助线程更新它们,我不知道如何从我的辅助线程传输数据,并制定一个从主线程更新它们的系统.我把我的代码放在下面:
任何帮助都会很棒.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace GUIBike
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static string inputdata;
public static int MaximumSpeed, maximumRiderInput, RiderInput, Time, CurrentSpeed, DistanceTravelled, MaximumMotorOutput, MotorOutput, InputSpeed;
public static string SaveDataString;
public Thread Serial;
public static SerialPort SerialData; …Run Code Online (Sandbox Code Playgroud) 可能重复:
从多个线程调用Console.WriteLine
只是想知道多个线程是否调用相同的console.writeline,它会导致死锁吗?
这是一个例子:
#include<iostream>
#include<thread>
using namespace std;
void f1(double& ret) {
ret=5.;
}
void f2(double* ret) {
*ret=5.;
}
int main() {
double ret=0.;
thread t1(f1, ret);
t1.join();
cout << "ret=" << ret << endl;
thread t2(f2, &ret);
t2.join();
cout << "ret=" << ret << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
ret=0
ret=5
Run Code Online (Sandbox Code Playgroud)
用gcc 4.5.2编译,有和没有-O2标志.
这是预期的行为吗?
这个节目数据是否免费比赛?
谢谢
关于Singletons的维基百科文章提到了一些在Java中实现结构的线程安全方法.对于我的问题,让我们考虑具有冗长初始化过程并且同时被许多线程所占据的单身人士.
首先,这个未提及的方法是否是线程安全的,如果是这样,它会同步什么?
public class Singleton {
private Singleton instance;
private Singleton() {
//lots of initialization code
}
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
其次,为什么以下实现线程在初始化时是安全且懒惰的?如果两个线程同时进入该getInstance()方法会发生什么?
public class Singleton {
private Singleton() {
//lots of initialization code
}
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在第二个示例中,如果一个线程首先获取实例而另一个线程获取实例并尝试在构造函数在第一个线程中完成之前对其执行操作,该怎么办?那你可以进入一个不安全的状态吗?
我目前正在编写我的第一个关于C#的程序,而且我对该语言非常陌生(迄今为止只用于C).我做了很多研究,但所有的答案都太笼统了,我根本无法解决问题.
所以这里是我(非常常见)的问题:我有一个WPF应用程序,它从用户填充的几个文本框中获取输入,然后使用它来对它们进行大量计算.它们应该花费大约2-3分钟,所以我想更新进度条和文本块,告诉我当前的状态.此外,我需要存储用户的UI输入并将它们提供给线程,所以我有第三个类,我用它来创建一个对象,并希望将此对象传递给后台线程.显然我会在另一个线程中运行计算,因此UI不会冻结,但我不知道如何更新UI,因为所有计算方法都是另一个类的一部分.经过大量的研究后,我认为最好的方法是使用调度员和TPL,而不是背景工作者,但说实话,我不确定它们是如何工作的,经过大约20个小时的试错和其他答案,我决定问我自己的问题.
这里有一个非常简单的程序结构:
public partial class MainWindow : Window
{
public MainWindow()
{
Initialize Component();
}
private void startCalc(object sender, RoutedEventArgs e)
{
inputValues input = new inputValues();
calcClass calculations = new calcClass();
try
{
input.pota = Convert.ToDouble(aVar.Text);
input.potb = Convert.ToDouble(bVar.Text);
input.potc = Convert.ToDouble(cVar.Text);
input.potd = Convert.ToDouble(dVar.Text);
input.potf = Convert.ToDouble(fVar.Text);
input.potA = Convert.ToDouble(AVar.Text);
input.potB = Convert.ToDouble(BVar.Text);
input.initStart = Convert.ToDouble(initStart.Text);
input.initEnd = Convert.ToDouble(initEnd.Text);
input.inita = Convert.ToDouble(inita.Text);
input.initb = Convert.ToDouble(initb.Text);
input.initc = Convert.ToDouble(initb.Text);
}
catch
{
MessageBox.Show("Some input values are not of …Run Code Online (Sandbox Code Playgroud) 我已经创建了一段代码,它接受一个IP地址(来自另一个类中的main方法),然后循环遍历一系列IP地址,并在每个IP地址上执行.我有一个GUI前端,它正在崩溃(因此我为什么要完成多线程.我的问题是我不能再将IP地址作为我的ping代码中的参数作为其可调用的.我已经搜遍了所有为此,似乎无法找到解决这个问题的方法.有一种方法可以让一个可调用的方法来获取参数吗?如果没有,有没有其他方法可以实现我想要做的事情?
我的代码示例:
public class doPing implements Callable<String>{
public String call() throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once …Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关Task.Yield,而且作为一名JavaScript开发,我可以告诉大家,就是它的工作是 完全相同一样setTimeout(function (){...},0);在让主单线程处理又名其他的东西方面:
"不要把所有的力量从时间上释放出来 - 所以其他人也会有一些......"
在js中,它特别适用于长循环.(不要让浏览器冻结......)
但我在这里看到了这个例子:
public static async Task < int > FindSeriesSum(int i1)
{
int sum = 0;
for (int i = 0; i < i1; i++)
{
sum += i;
if (i % 1000 == 0) ( after a bulk , release power to main thread)
await Task.Yield();
}
return sum;
}
Run Code Online (Sandbox Code Playgroud)
作为JS程序员,我可以理解他们在这里做了什么.
但作为一名C#程序员,我问自己:为什么不为它开一个任务呢?
public static async Task < int > FindSeriesSum(int i1)
{
//do something.... …Run Code Online (Sandbox Code Playgroud)