Categories
discuss

Are there thread group-local variables in Java?

I am looking for a class similar to ThreadLocal which would work on thread groups instead of threads.

If there is not such a class (in some open source library) how would you implement it? Some better idea than having thread groups in WeakHashMap?

I am implementing a debugging framework tunable in run-time with various parameters in global, per-thread and per-thread group contexts. As a very simple example, you can have a reporting statement:

debug.log( category, message);

and specify that the log entry with that specific category will be displayed only when called by a thread in group of threads servicing network requests.

Answer

I would store a value holder in a thread local and initialize it to the same value holder for all threads of the same group.

 public class ThreadGroupLocal<T> extends ThreadLocal<ValueHolder> {
     private static class ValueHolder {
         public Object value;
     }
     // Weak & Concurrent would be even the better, but Java API wont offer that :(
     private static ConcurrentMap<ThreadGroup, ValueHolder> map = new ConcurrentHashMap<ThreadGroup, ValueHolder>;
     private static ValueHolder valueHolderForThread(Thread t) {
         map.putIfAbsent(t.getThreadGroup(), new ValueHolder());
         return map.get(t.getThreadGroup());
     }
     @Override 
     protected ValueHolder initialValue() {
         return valueHolderForThread(Thread.currentThread());
     }
     public T getValue() { (T) get().value; }
     public void setValue(T value) { get().value = value; }
 }

and then use

 ThreadGroupLocal<String> groupLocal = new ThreadGroupLocal<String>();
 groupLocal.setValue("foo");
 //...
 String foo = groupLocal.getValue();

That does (expect for the initialization) perform exactly like a thread local.

Categories
discuss

asp.net ensure javascript is loaded once only

I’ve been working in asp.net webforms and I’ve been making a UserControl that depends on a small bit of javascript which is in an external file.

I want to be able to put a reference to the javascript inside the UserControl to ensure that it gets loaded on the page, but the problem is that the UserControl can appear multiple times on a page so I only want the script to be loaded with the first instance of the UserControl.

Is there an easy way to do this in ASP.NET Webforms?

Answer

Use the ClientScriptManager‘s RegisterClientScript() methods to handle this. In your case, since you’re including the script from an external file, use RegisterClientScriptInclude().

Alternately, if you’re using UpdatePanels, use ScriptManager‘s equivalent Register...() methods.

Categories
discuss

does continue work in a do while?

I have a do while that looks like:

User user = userDao.Get(1);

do
{
 // processing


 // get the next user
 //
 user = UserDao.GetNext(user.Id);

 if(user == null)
       continue;   // will this work?????????????
}
while ( user != null)

If it does work, its going to go to the top of the do statement, and user is null so things are going to break?

Maybe I should rework the loop to a while statement?

Answer

The continue makes it jump to the evaluation at the botton so the program can evaluate if it has to continue with another iteration or exit. In this case it will exit.

This is the specification: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#6045

Such language questions you can search it in the Java Language Specification: http://java.sun.com/docs/books/jls/

Categories
discuss

Rails, jQuery, .js.erb files, JS not executed by the browser

I’m trying to use jQuery, and everything has been great, until now, when I’m trying to render a partial and append it to a div. Here is how I have it set up:

I have an action that responds to js:

def index
  @objects = Object.find(:all)

  respond_to do |format|
    format.js
  end
end

And a template called index.js.erb with some javascript in it:

alert("hello world");

Firebug returns a “text/javascript” response containing:

alert("hello world");

But the alert window does not appear, nor does any other JavaScript work. I checked out http://railscasts.com/episodes/136-jquery And am more or less following along. I tried several other things, like using .rjs instead of .erb and putting this in my template:

page.alert("hello world"); 

but I get the same exact result, and the browser never executes the JS.

Anyone know why the JavaScript isn’t being executed?

I’m running Rails 2.3.4.

Answer

You have to call it from your view or it will never be executed.

an example controller:

def index
  @objects = Object.find(:all)

  respond_to do |format|
    format.js{
      render :text => "alert('hello')"
    }
  end
end

and an index.html.erb with:

<script type="text/javascript">
  $(function(){
    $.ajax({ url: '/controller', type: 'get', dataType:'script' });
  });
</script>

replace ‘/controller’ with the actual url that executes that controller’s index action, by default for PostsController it will be ‘/posts’ and so on…

if you like rjs you delete the {} and everithing in it in the controller and create an index.js.erb or index.rjs with:

page.alert("hello world")

or:

page << "hello world"
Categories
discuss

Debugging Android Widget Code In Eclipse

I am starting with Android programming with the help of Unlocking Android / Manning which came from Santa and is a pretty good book. After running a few little tests and examples, I’m moving towards developing the concepts for my own application.

I want my App to execute as a Home Widget and I’m using this example as my model to get started.

While I am able to load and run in the Android ADB emulator the code example in the example’s Wiktionoary which is a normal Android App. I am not able to run, but only load into the emulator the Widget version of that code named SimpleWiktionary.

Am I am able to debug Android Home Widgets with the emulator? If so, what am I missing on starting/activating the Widget or having it show up in the Apps window?

T, Kenny

Answer

Widgets don’t show up with icons like most other applications would. To add one to the home screen, long press on the background wallpaper and you should see a context menu pop-up that will allow you to add a widget to the screen. If you AndroidManifest.xml is set up correctly, your widget should be shown in the menu.

You should be able to debug it by setting breakpoints in Eclipse like you would for debugging any other Android app. However, if your widget isn’t displayed on the home screen, none of the code will be executed.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..