Go to Post Maybe FIRST made these bumper rules so IRI would have something to blow off come July. - Koko Ed [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 26-01-2011, 21:01
flameout flameout is offline
AKA Ryan Van Why
FRC #0957 (SWARM)
Team Role: Alumni
 
Join Date: Sep 2009
Rookie Year: 2009
Location: Oregon
Posts: 168
flameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to all
Netbeans doesn't like Thread.yield()

When ever I try to use Thread.yield (in my vision thread), NetBeans complains with "invoking yield() on java.lang.Thread" (just a warning, not an error).

I'd prefer to have code with no warnings, and am concerned that I'm not doing this right.

Here's the faulty class. Another class creates the thread object and calls start() on it.
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.image.ColorImage;
import edu.wpi.first.wpilibj.image.BinaryImage;

public class Vision implements Runnable {
	AxisCamera    axis;
	ColorImage    colimage;
	MonoPosterize posterize;
	BinaryImage   binimage;
	Thread        thread;

	// Updates the target positions if a new image exists.
	public void refresh() {
		try {
			if (colimage != null) colimage.free();
			colimage = axis.getImage();
			if (binimage != null) binimage.free();
			binimage = posterize.posterize(colimage);
			System.out.println(binimage.getNumberParticles());
			System.out.println(binimage.getParticleAnalysisReport(0));

		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	private void WaitForImage() {
		while (!axis.freshImage()) {
			Thread.yield();
		}
	}
	
	// This is run by Thread.start();
	public void run() {
		thread = Thread.currentThread();
		thread.setPriority(Thread.MIN_PRIORITY);
	}
}
Can someone verify that I'm coding this correctly?
Reply With Quote
  #2   Spotlight this post!  
Unread 26-01-2011, 21:59
sjspry sjspry is offline
Registered User
FRC #1984
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Kansas
Posts: 125
sjspry has a spectacular aura aboutsjspry has a spectacular aura aboutsjspry has a spectacular aura about
Re: Netbeans doesn't like Thread.yield()

You are [coding it correctly]. Hit Alt+Enter on one of the Thread.yield() lines and turn the warning off.

NetBeans complains as Thread.yield() is sometimes used incorrectly. Instead of being used to prevent threads from starving (having one thread take most of the CPU time) like it should be, it can be used with varying degrees of effectiveness to make a statement look atomic (in one piece), e.g. have the thread yield before some operation, to (try to) keep it from being forced to yield in the middle of that operation (which could cause concurrency issues).

If you have to ask, don't worry about it. If it really bugs you and you don't want to just turn it off, you could make your thread sleep for an appropriate amount of time.
Reply With Quote
  #3   Spotlight this post!  
Unread 26-01-2011, 22:45
Jeanne Boyarsky Jeanne Boyarsky is offline
Java Mentor
FRC #0694 (StuyPulse)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2010
Location: New York
Posts: 100
Jeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud ofJeanne Boyarsky has much to be proud of
Re: Netbeans doesn't like Thread.yield()

Thread.yield and Thread.sleep do different things. Thread.yield says to give others a turn. Thread.sleep says don't even think about getting back to me for X milliseconds.
__________________
Team 694 mentor 2010-present, FIRST Volunteer and Co-organizer of FIRST World Maker Faire Tent
2012 NYC Woodie Flowers Finalist
2015 NYC Volunteer of the Year
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2011, 22:50
sjspry sjspry is offline
Registered User
FRC #1984
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Kansas
Posts: 125
sjspry has a spectacular aura aboutsjspry has a spectacular aura aboutsjspry has a spectacular aura about
Re: Netbeans doesn't like Thread.yield()

Yes, but when a thread sleeps the scheduler moves on to the next task. The greatest resolution you can expect is somewhat greater than a millisecond, more like sixteen (iirc) due to task switching (if it happens) and other threads running.

It's probably true that sleeping for only a few ms is a bit slim... I would suggest something more like 50, if you can spare it. But this is only if he doesn't want to turn off the warning that bugs him.
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2011, 23:27
flameout flameout is offline
AKA Ryan Van Why
FRC #0957 (SWARM)
Team Role: Alumni
 
Join Date: Sep 2009
Rookie Year: 2009
Location: Oregon
Posts: 168
flameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to allflameout is a name known to all
Re: Netbeans doesn't like Thread.yield()

Then I'll turn off that warning... I know how to use Thread.yield() (correctly) :-)

Thanks for the help.

EDIT: clarification
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:58.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi