使用显示器的Writer / Reader不工作(Writer/Reader using Monitor not working)
我正在做一个任务,在哪里实现一个使用监视器进行同步的Writer / Reader问题。
场景:作者应该从列表中一次一个地写入一个字符串到缓冲区中(缓冲区一次只能保存一个字符串)。 然后阅读器应该从缓冲区中读取字符串,并在打印之间暂停0.5秒后在gui中打印字符串。 该程序使用一个单独的编写器线程和一个单独的读取器线程,当gui中的一个按钮被按下时,这些线程同时启动。
我的问题是:我无法使同步工作,读者只需每0.5秒输出一个空值(这意味着当阅读器试图从中读取时缓冲区很可能是空的)。 我设法发现一个错误:作家没有达到
try
buffer.put(text.get(i))
,尽管我找不出原因。任何人可以帮助? 非常感激! 我的代码在下面,希望它够了,我不想垃圾太多的代码。 (我也很喜欢Writer / Reader问题,但已经阅读了一些编码示例等)
作家:
public void run() { buffer.length(text.size()); for (int i = 0; i < text.size(); i++) { //loops thorugh the list of strings System.out.println("hj"); //reaches this try { buffer.put(text.get(i)); //put the string into the buffer System.out.println("put done"); // never reaches this sleep(); //pauses the thread for 0.5 seconds } catch (InterruptedException e) { System.out.println("error"); //never reaches this either e.printStackTrace(); } } }
缓冲:
/** * gets a string from the writer * @throws InterruptedException */ public synchronized void put(String string) throws InterruptedException { while(hasString == false) { //we only want to put a string into the buffer if it is empty wait(); this.string = string; hasString = true; notify(); } } /** * passes on a string to the reader * @throws InterruptedException */ public synchronized String get() throws InterruptedException { while(hasString == true) { //we only want to pass on a string if the buffer is NOT empty wait(); hasString = false; notify(); } return string; }
读者:
/** * reads a string from the buffer and writes it to the gui in the destination-tab */ public void run() { for (int i = 0; i < buffer.getLength(); i++) { //loops through the length of the list with strings try { string = buffer.get(); //gets string from buffer } catch (InterruptedException e1) { e1.printStackTrace(); } //writes the string in the gui sb.append(string); GUIMonitor.txtPaneDest.setText(sb.toString()); try { sleep(); //pauses the thread for 0.5 seconds } catch (InterruptedException e) { e.printStackTrace(); } } }
I´m doing an assignment where are implementing a Writer/Reader problem that uses a monitor for synchronization.
The scenario: The writer should write a string from a list one at a time into a buffer (the buffer can only hold one single string at a time). The reader should then read the string from the buffer and print the string in a gui with a 0.5 second pause between the prints. The program uses one singular writer-thread and one singular reader-thread, which are started simultaneously when a button in the gui is pressed.
My problem: I cannot get the synchronization to work, the reader just prints out null every 0.5 seconds (which means the buffer is most likely empty when the reader tries to read from it). I managed to find an error: the writer is not reaching the
try
wherebuffer.put(text.get(i))
, though I cannot figure out why.Anyone who could help?? Much appreciated! My code is down below, hope it is enough, I don´t want to spam too much code. (I´m also pretty new with the Writer/Reader problem but have read some coding examples etc.)
Writer:
public void run() { buffer.length(text.size()); for (int i = 0; i < text.size(); i++) { //loops thorugh the list of strings System.out.println("hj"); //reaches this try { buffer.put(text.get(i)); //put the string into the buffer System.out.println("put done"); // never reaches this sleep(); //pauses the thread for 0.5 seconds } catch (InterruptedException e) { System.out.println("error"); //never reaches this either e.printStackTrace(); } } }
Buffer:
/** * gets a string from the writer * @throws InterruptedException */ public synchronized void put(String string) throws InterruptedException { while(hasString == false) { //we only want to put a string into the buffer if it is empty wait(); this.string = string; hasString = true; notify(); } } /** * passes on a string to the reader * @throws InterruptedException */ public synchronized String get() throws InterruptedException { while(hasString == true) { //we only want to pass on a string if the buffer is NOT empty wait(); hasString = false; notify(); } return string; }
Reader:
/** * reads a string from the buffer and writes it to the gui in the destination-tab */ public void run() { for (int i = 0; i < buffer.getLength(); i++) { //loops through the length of the list with strings try { string = buffer.get(); //gets string from buffer } catch (InterruptedException e1) { e1.printStackTrace(); } //writes the string in the gui sb.append(string); GUIMonitor.txtPaneDest.setText(sb.toString()); try { sleep(); //pauses the thread for 0.5 seconds } catch (InterruptedException e) { e.printStackTrace(); } } }
原文:https://stackoverflow.com/questions/47968345
最满意答案
正如他们的名字所暗示的,
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()
和UISaveVideoAtPathToSavedPhotosAlbum()
方法UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()
一个路径 ,而不是一个URL。 不是一回事。字符串
file:///private/var/mobile/Containers/Data/Application/.../filename.mov
是一个URL ,由该方案组成
file://
和路径
/private/var/mobile/Containers/Data/Application/.../filename.mov
传递完整的URL时,设计为接受路径的函数将无法正常工作。
要解决该问题,请使用
videoURL.path
替换对videoURL.absoluteString
调用。As their names suggest, the methods
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()
andUISaveVideoAtPathToSavedPhotosAlbum()
expect a path, not a URL. Not the same thing.The string
file:///private/var/mobile/Containers/Data/Application/.../filename.mov
is a URL, composed of the scheme
file://
and path
/private/var/mobile/Containers/Data/Application/.../filename.mov
A function designed to accept a path will not work correctly when passed a complete URL.
To solve the problem, replace calls to
videoURL.absoluteString
withvideoURL.path
.
相关问答
更多-
从NSBundle硬编码视频迁移,以替代图像选择器文件(Migrating from NSBundle hardcoded video to allow for image picker file instead)[2023-02-26]
本地保存视频什么是您需要的合作伙伴,您可以将它发送到您的服务器 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; NSData *videoData = [NSData ... -
正如他们的名字所暗示的, UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()和UISaveVideoAtPathToSavedPhotosAlbum()方法UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()一个路径 ,而不是一个URL。 不是一回事。 字符串 file:///private/var/mobile/Containers/Data/Application/.../filename.mov 是一个URL ,由该 ...
-
通过将此代码放入我的通用处理程序页面(我在customErrors中设置),我找到了解决此问题的任何人的快速方法 if (Request.Params.ToString().Contains("jpg") || Request.Params.ToString().Contains("png")) Response.StatusCode = 404; 这基本上迫使它为任何jpeg / png图像返回404,但是对于所有其他错误,它会强制显示通用页面。 I found a quick way ...
-
我把这个答案写给那些不想使用奏鸣曲分类包和奏鸣曲媒体包(像我一样)的人,所以必须使用4.x-dev。 我做了一个提交 ,使它与symfony3兼容(似乎)。 希望这有帮助。 I write this answer to people who don't want to use sonata classification bundle with sonata media bundle (like me) so have to use 4.x-dev. I make a commit to make it co ...
-
使用变量进行结构访问时引用不存在的字段(Reference to non-existent field when using variable for struct access)[2022-11-01]
你非常接近,只需从变量名称周围删除单引号,如下所示: value = S.(name).structA.SubstructA1.variableA You are very close, just remove the single quotes from around the variable, name, like this: value = S.(name).structA.SubstructA1.variableA -
django静态文件加载将不存在的目录添加到文件路径(django static file loading adds a non-existent directory to the file path)[2022-01-03]
尝试使用该解决方案在开发期间提供静态文件 : from django.conf import settings from django.conf.urls.static import static urlpatterns = [ .... ] if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 问题解决了: 您的STATIC_URL应为STA ... -
Azure Web应用程序在引用不存在的路径时抛出错误(Azure web app throwing error when referring to non-existent path)[2023-10-31]
您是否搜索过实际代码以获取对该文件的引用? 或者它以前是否已经存在并且机器人试图重新阅读它? Have you searched your actual code for a reference to that file? Or would it previously have been there and a bot is trying to re-read it? -
使用fetch blob和image picker对本机上传视频做出反应(react native upload video with fetch blob and image picker)[2022-04-02]
你的代码有三个问题... 文件格式应为mp4,并且您在文件名中给出扩展名.png。 包裹你的路径。 不需要在有效载荷中指定类型。 检查下面给出的例子 ImagePicker.showImagePicker(options, (response) => { if (response.didCancel) { } else if (response.error) { } el ... -
你还没有写过如何注入/调用你的服务,但是调用$this->get()听起来像是来自控制器内的调用。 我想这与Symfony的新变化和public属性的默认服务配置有关。 请检查配置中的以下注释行: # services.yml services: _defaults: autowire: true autoconfigure: true public: false # here you are setting all service per defau ...
-
你可以使用相机插件org.apache.cordova.camera并使用这个选项从图库中选择一个视频(在Android中测试): var options = { quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.PHOTOLIBRARY, mediaType:Camera.MediaType.VIDEO }; ...