Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
There seems to be a lot of fuss about multicore and java. While some people say that java support is not good enough, it definitely seems to be an area to look forward to. There seems to be many techniques to improve performance of concurrent programs. Any tips/advices about programming in a multi core scenario is appreciated.
I know anonymous classes save typing, if it comes to implementing Listener and similar stuff. They try to be a replacement for some usages of closures. But what does the community think about the value of these language-feature? Does it make sense and you use it regularly? Makes the saved typing the code clearer, more understandable and more maintainable? Or does anonymous classes make the cod...
What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a wakelock. Regards, Shafqat
Greetings , Can anyone suggest to me how I can pass a parameter to a thread. I can't seem to find anything about it! I'm sure this is quite simple. [Edit] Apologies I was using anonymous classes (Which was the problem) Hit the refactor button :) [/Edit]
1]What is Non-blocking Concurrency and how is it different than Normal Concurrency using Threads. Why dont we use "non-blocking" Concurrency in all the scenarios where concurrency is reuired.. are there any overheads for "non-blocking" 2] I have heard that this is available in Java. Are there any particular scenarios we should use this feature 3] Is there a difference/advantage of using one o...
I am stuck at some point wherein I need to get database changes in a Java code. Request is to get any record updated, added, deleted in any table of db; should be recognized by Java program. How could it be implemented JMS? or a Java thread? Update: Thanks guys for your support i am actually using Oracle as DB and Weblogic 10.3 workshop. Actually I want to get the updates from a table in which...
What list of verbs are you using for method names? What's your personal or team standard? I debate whether to use Do vs. Run vs. Execute vs. Perform and am wondering if any of these are no longer recommended or some that people just don't really use and I should just scratch them. Basically any one of those verbs mean the same thing...to invoke some process (method call). This is outside of...
What are Java's equivalents of Func and Action? I mean, instead of writing this on my own: public interface Func<TInput, TResult> { TResult call(TInput target) throws Exception; } public interface Action<T> { void call(T target) throws Exception; }
I realize that the method run() must be declared because its declared in the Runnable interface. But my question comes when this class runs how is the Thread object allowed if there is no import call to a particular package? how does runnable know anything about Thread or its methods? does the Runnable interface extend the Thread class? Obviously I don't understand interfaces very well. th...
Because the JDK doesn't contain them, a zillion of 3rd party libraries contain Action or Func interfaces, that look something like: public interface Action<T> { void doSomething(T item); } public interface Func<TResult, TInput> { TResult doSomething(TInput input); } What is the de-facto, or most commonly used standard for this?
I've used Java for some time and I keep hearing about interfaces such as Cloneable, Iterable and other X-ables. I was wondering if there is a list somewhere of all of these and more importantly - which ones do you regularly use day-to-day? For example, I've read that Cloneable is considered badly written and isn't widely used.
So far I've been using "public void run() {}" methods to execute my code in Java. When/why might one want to use main() or init() instead of run()?
When a Thread is finished, you cannot run it once more, using start() method: it throws an Exception. Could anyone explain, why? What stands behind such an architectural decision?
I am just curious on how people solve this. I often write the same type of code all the time. For instance: new Thread() { //... //... //... //Change this line //... //... }.start(); I keep changing the line where it says "Change this line" and then starting a thread. This change can be one line or a few lines. How would I go about compacting this code?
Standard Runnable interface has only non-parametrized run() method. There is also Callable<V> interface with call() method returning result of generic type. I need to pass generic parameter, something like this: interface MyRunnable<E> { public abstract void run(E reference); } Is there any standard interface for this purpose or I must declare that basic one by myself?
I want to retrieve from subscription and store feeds to DB from subscription after every 6 hours. I want to have a timer thread in background to accomplish this task. What's the best way? A normal timer thread or Quartz API?
Does anyone have any insight into the history of Java Thread class's run() method being public? Almost all the time, it gets used by overriding and thus would the protected modifier have been more appropriate? That would still leave the start() as the public api for users and thus not leave any room for mistakes with users calling run() accidentally.
This topic expands on When do/should I use __construct(), __get(), __set(), and __call() in PHP? which talks about the __construct __get and __set magic methods. As of PHP 5.3 there is a new Magic Method called __invoke. The __invoke method is called when a script tries to call an object as a function. Now on research I have done for this method, people liken it to the Java method .run() - s...
I've got a stand alone, headless, java server app that does a bunch of queue-based processing against a database that I'm thinking of migrating into a java app server. I've got plenty of back-end java experience and a bit of JSP, but not a lot of servlet experience. It seems like the approach would be to just wrap my app into a servlet and have it deploy on startup (and make sure that it only...
I have developed a web application using using servlet and JSP. I am not using any framework per se ... instead using my own home brewed MVC framework I am using MySQL as a backend. I want to do the following cleanup some data from the data base every hour generate and store statistics about data every 15 minutes in an XML file somewhere the problem is ... currently all my code runs as a...
 /*
  * Copyright 1994-2005 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang;

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Author(s):
Arthur van Hoff
Since:
JDK1.0
See also:
Thread
java.util.concurrent.Callable
public
interface Runnable {
    
When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

The general contract of the method run is that it may take any action whatsoever.

See also:
Thread.run()
    public abstract void run();
New to GrepCode? Check out our FAQ X