首页 \ 问答 \ 线程中断替代?(Thread interrupt alternatives?)

线程中断替代?(Thread interrupt alternatives?)

使用SOS模式构建Flashlight应用程序。 有3个按钮(开,关和SOS)。 应用程序在正常的开启和关闭模式下工作,但在SOS模式下不工作。(SOS模式不关闭)

//this method gets called when Off button is pressed
    private void turnOffFlash() {
            if (FlashOn) {
                if (myCamera == null || myParameters == null) {
                    return;
                }
                myParameters = myCamera.getParameters();
                myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                myCamera.setParameters(myParameters);
                myCamera.stopPreview();
                try {
                    if (SOSon)
                        Flashthread.interrupt();
                    SOSon = false;
                } catch (Exception ex) {
                    throw ex;
                }
                FlashOn = false;
                number_of_press=0;
            }
        }

这里使用Flashthread

void onSOSPress() {
        if (number_of_press == 1) {
            try {
                SOSon = true;
                if (!Flashthread.isInterrupted()) {
                    if (SOSon) {
                        Flashthread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                for (int i = 0; i < System.currentTimeMillis(); i++) {
                                    if (FlashOn) {
                                        myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                                        myCamera.setParameters(myParameters);
                                        FlashOn = false;
                                    } else {
                                        TurnOnFlash();
                                    }
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                        });
                        Flashthread.start();
                    }
                } else
                    Flashthread.resume();
            } catch (Exception ex) {
                throw ex;
            }
        }
    }

turnOffFlash方法中,由于我读到中断方法并没有真正“中断”/杀死线程,我可以使用什么而不是Thread.Interrupt(); 按下Off按钮是否会停止SOS模式? 我尝试了stop()destroy()但两者都崩溃了应用程序。


Building a Flashlight App with SOS mode. Has 3 buttons( On, Off and SOS). App works in normal On and Off mode but not in SOS mode.(SOS mode doesnt switch off)

//this method gets called when Off button is pressed
    private void turnOffFlash() {
            if (FlashOn) {
                if (myCamera == null || myParameters == null) {
                    return;
                }
                myParameters = myCamera.getParameters();
                myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                myCamera.setParameters(myParameters);
                myCamera.stopPreview();
                try {
                    if (SOSon)
                        Flashthread.interrupt();
                    SOSon = false;
                } catch (Exception ex) {
                    throw ex;
                }
                FlashOn = false;
                number_of_press=0;
            }
        }

and the Flashthreadis used here

void onSOSPress() {
        if (number_of_press == 1) {
            try {
                SOSon = true;
                if (!Flashthread.isInterrupted()) {
                    if (SOSon) {
                        Flashthread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                for (int i = 0; i < System.currentTimeMillis(); i++) {
                                    if (FlashOn) {
                                        myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                                        myCamera.setParameters(myParameters);
                                        FlashOn = false;
                                    } else {
                                        TurnOnFlash();
                                    }
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                        });
                        Flashthread.start();
                    }
                } else
                    Flashthread.resume();
            } catch (Exception ex) {
                throw ex;
            }
        }
    }

In the turnOffFlash method,since I read that the Interrupt method doesn't really "interrupt"/kill the thread,what can I use instead of Thread.Interrupt(); so that pressing the Off button stops the SOS mode? I tried the stop() and destroy() but both crashed the app.


原文:https://stackoverflow.com/questions/39055659
更新时间:2023-04-05 14:04

最满意答案

您正在调整使用IDE创建字体工具定义其大小的字体 。 您可以使用createFont()函数代替loadFont() ,该函数用于从内置字体之一( Serif,SansSerif,Monospaced,Dialog,DialogInput )创建字体,该字体映射到每个操作的默认字体系统。

使用createFont()的一个优点是文本在任何大小都会看起来很平滑,取消与loadFont()一起使用的字体,在调整大小时可能会失真。 使用createFont()来指定除内置字体之外的东西是可能的,但不能保证字体将安装在另一个用户系统上。 - Ben Fry,可视化数据

以下代码在屏幕上显示“p5”:

PFont font;

void setup () {
size(500,500);
font = createFont ("Serif",height);
textFont (font);
}

void draw () {
background(255);  
fill(0);
textAlign(CENTER,CENTER);
text("p5",width/2,height/2);
}

You're resizing a font whose size was defined using the IDE create font tool. Instead of loadFont(), you could use the createFont() function, which is used to create a font from one of the built-in typefaces (Serif, SansSerif, Monospaced, Dialog, DialogInput) that map to the default fonts on each operating system.

One advantage of using createFont () is that the text will look smooth at any size, unlinke a font used with loadFont (), which may be distorted as it is resized. It's possibile to use createFont () to specify something besides a built-in font, but there's no guarantee that the font will be installed on another's user system. – Ben Fry, Visualizing Data

The following code displays "p5" on the screen:

PFont font;

void setup () {
size(500,500);
font = createFont ("Serif",height);
textFont (font);
}

void draw () {
background(255);  
fill(0);
textAlign(CENTER,CENTER);
text("p5",width/2,height/2);
}

相关问答

更多
  • 手头有两个即时问题 首先,需要读取文件,以便可以逐步更新UI而不会造成无法接受的延迟 其次, JTextArea能够实际处理这些数据量...... 第一个问题相对而言很容易解决。 您需要确保的是,在您读取文件时并未阻止事件派发线程,并且您只是在事件派发线程的上下文中更新JTextArea 。 为此, SwingWorker是一个很好的选择,例如... public class FileReaderWorker extends SwingWorker, String> { ...
  • 问题是你设置了loadingCircleFF.Visible = true;之后你的UI就没有机会重新绘制自己loadingCircleFF.Visible = true; 在将工作项排队到工作线程之后,您的UI线程正在忙于处理RSS.DeleteByGroup(RSSGroupID); 和DTcancel_RSS(null, null); ,所以它不会立即显示。 在我更新的代码中,我将它们放在QueueUserWorkItem的主体中,因此它们不会阻止您的UI线程。 注意到工作项在工作线程上运行,因此您的 ...
  • 主要有两个步骤: 获取深度(KinectTracker已在track()方法中执行) 使用偏移来获取当前像素的深度,以基于2D位置(x,y)在1D深度阵列中找到位置(这也是在track()方法中完成的: int offset = kw-x-1+y*kw; ) 请注意,坐标是镜像的,通常对索引进行计算,如下所示: index = y*width+x 如get()参考说明中所述 所以在理论上你需要的东西就是这样在track()方法的末尾: lerpedLoc.z = depth[kw-((int)lerped ...
  • 您正在调整使用IDE创建字体工具定义其大小的字体 。 您可以使用createFont()函数代替loadFont() ,该函数用于从内置字体之一( Serif,SansSerif,Monospaced,Dialog,DialogInput )创建字体,该字体映射到每个操作的默认字体系统。 使用createFont()的一个优点是文本在任何大小都会看起来很平滑,取消与loadFont()一起使用的字体,在调整大小时可能会失真。 使用createFont()来指定除内置字体之外的东西是可能的,但不能保证字体将安 ...
  • 你将不得不做一些重塑,但另一个选择是你可以使用fread。 但正如所提到的,这基本上将你锁定为矩形导入。 所以另一个选择是使用textscan。 正如我在另一篇文章中提到的,我不是百分之百确定实施的时候,我所知道的是你没有“importdata()” fid = fopen('textfile.txt') Out = textscan(fid,'%s','delimiter',sprintf('\n')); fclose(fid) 通过使用文本扫描,您将能够获得每行的单元格数组,然后您可以根据需要进行操 ...
  • 您可以将uicontrol文本字段的位置和大小设置为数字的中心: decodedValue = 'Decoded Value: 2'; fig = figure(3); hPan = uipanel(fig,'Units','normalized'); uicontrol(hPan, 'Style','text', 'HorizontalAlignment','center', ... 'FontSize',25, 'Units','normalized', 'Position',[0.2 0.4 0. ...
  • 请注意,您的球体半径为40,这意味着直径为80,并且您将文本和球体绘制在相同的水平位置(500)。 将文本向右移动以避免球体遮挡它: void setup() { size(720, 546, P3D); background(0); } void draw() { lights(); pushMatrix(); translate(500, height*0.35, 0); fill(236, 112, 20); noStroke(); sphere(40) ...
  • 我很遗憾地说,但最好的办法是不要使用文本框,而是获得一个编辑器组件(例如SyntaxEdit),它是为部分更新和处理大量文本而设计的。 UI由于您的处理而没有锁定,但是因为文本框对于大文本而言效率不高,并且文本框在更新时阻止UI线程。 没有重写就行不通。 你要比它的规格大14.900行。 每次更新框中的文本时,它都会启动一个非常慢的重绘循环。 一个你不能打断,所以所有线程都没有帮助 - 因为在这个更新周期结束之前,UI是无响应的。 “真实文本编辑器”还有其他组件,它们也准备处理更大的文本。 I am sor ...
  • 你所描述的是处理createFont的正确行为。 直接从文档中引用: 的createFont(): 使用计算机上安装的字体名称或草图“data”文件夹中的.ttf或.otf文件将字体动态转换为Processing使用的格式。 此功能是精确控制的高级功能。 在大多数情况下,您应该通过从“工具”菜单中选择“创建字体...”来创建字体。 通过处理应用程序创建vlw字体文件可以更好地处理您要做的事情:menu / tools / Create Font ...(将结果文件复制到应用程序的数据文件夹中) PFont ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。