Search Results

Search found 514 results on 21 pages for 'runnable'.

Page 1/21 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Runnable to be run every second only runs once in Fragment onCreateView()

    - by jul
    I'm trying to update the time in a TextView with a Runnable supposed to be run every second. The Runnable is started from a Fragment's onCreateView(), but it's only executed once. Anybody can help? Thanks public class MyFragment extends Fragment { Calendar mCalendar; private Runnable mTicker; private Handler mHandler; TextView mClock; String mFormat; private boolean mClockStopped = false; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.meteo_widget, container, false); /* * Clock (from DigitalClock widget source) */ mClock = (TextView) view.findViewById(R.id.clock); mCalendar = Calendar.getInstance(); mHandler = new Handler(); mTicker = new Runnable() { public void run() { if(mClockStopped) return; mCalendar.setTimeInMillis(System.currentTimeMillis()); mClock.setText(DateFormat.format("hh:mm:ss", mCalendar)); mClock.invalidate(); long now = SystemClock.uptimeMillis(); long next = now + (1000 - now % 1000); mHandler.postAtTime(mTicker, next); } }; mTicker.run(); return view; } @Override public void onResume() { super.onResume(); mClockStopped = true; } @Override public void onPause() { mClockStopped = false; super.onPause(); } }

    Read the article

  • Eclipse Project Files to .exe or Runnable Jar

    - by twodayslate
    I am able to create the runnable jar using the export feature. However, it does not behave like it does in eclipse. When my program first starts, launch settings are displayed. I can see this. However, when I want to actually launch the meat of the program, it doesn't show up. It seems to not look at all my class files or something. I have tried Far Jar but that didn't work... I have triend JSmooth but still just ran the one frame If I can make this a runnable jar that works or even a .exe, that would be great!

    Read the article

  • Explicit call of Runnable.run

    - by klaudio
    Hi, I have a question. Somebody, who was working on my code before me, created some method and passed Runnable as parameter, more likely: void myMethod(Runnable runnable){ runnable.run(); } Then calling myMethod out of main looks like: public static void main(String args[]) { try { myMethod(new Runnable(){ public void run() { //do something...; }}); } catch (Throwable t) { } } So, to supply parameter to myMethod I need to instantiate object of (in this case anonymous) class implementing Runnable. My question is: is it necessary to use Runnable in this example? Can I use any different interface? I mean I can create new interface with single method i.e. interface MyInterface{ void doThis(); } then change look of myMethod: void myMethod(MyInterface myObject){ myObject.doThis(); } And of course client too: public static void main(String args[]) { try { myMethod(new MyInterface (){ public void doThis() { //do something...; }}); } catch (Throwable t) { } } Or maybe something is about Runnable?!

    Read the article

  • Spring - scheduling and pooling runnables of different state (each Runnable instance has different

    - by lisak
    Hi, I can't figure out what to use for scheduling and pooling runnables of different state (each Runnable instance has different state). I could use ScheduledExecutorFactoryBean together with MethodInvokingRunnable to supply arguments. But take a look at the key ScheduledExecutorFactoryBean method, it is designed in a way that all task should start at the beginning. protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor) { for (ScheduledExecutorTask task : tasks) { Runnable runnable = getRunnableToSchedule(task); if (task.isOneTimeTask()) { executor.schedule(runnable, task.getDelay(), task.getTimeUnit()); } else { if (task.isFixedRate()) { executor.scheduleAtFixedRate(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit()); } else { executor.scheduleWithFixedDelay(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit()); } } } } I can't think of how to setup this scenario with ThreadPoolTaskScheduler. Please help me out here. Thank you EDIT: shortened version is: how to setup task scheduler that would run hundreds of "different instances of Threads (with different state) with 2 seconds interval.

    Read the article

  • java: relationship of the Runnable and Thread interfaces

    - by Karl Patrick
    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. thanks in advance. class PrimeFinder implements Runnable{ public long target; public long prime; public boolean finished = false; public Thread runner; PrimeFinder(long inTarget){ target = inTarget; if(runner == null){ runner = new Thread(this); runner.start() } } public void run(){ } }

    Read the article

  • two threads acting on the same runnable

    - by Eslam
    Given: public class Thread1 { int x = 0; public class Runner implements Runnable { public void run() { int current = 0; for (int i = 0; i < 4; i++) { current = x; System.out.print(current + " "); x = current + 2; } } } public void go() { Runnable r1 = new Runner(); new Thread(r1).start(); new Thread(r1).start(); } public static void main(String[] args) { new Thread1().go(); } } Which two are possible results? (Choose two) A. 0, 2, 4, 4, 6, 8, 10, 6, B. 0, 2, 4, 6, 8, 10, 2, 4, C. 0, 2, 4, 6, 8, 10, 12, 14, D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14, i chosed A,B but i'm not certain is those is the true or not.

    Read the article

  • Unable to export runnable jar, launch configuration grayed out

    - by user13107
    I am not able to figure out how to export a runnable jar in eclipse. I have a java project (project A) (written by someone else) which when imported in Eclipse, I can click Build Project and it will create a projectName.jar file under bin/ directory. That jar file contains binary *.class files. This jar file is added as external library for another java project (project B) which I want to debug. But because all the class files are binary I'm not able to do line-by-line debugging. I tried exporting Runnable Jar in Eclipse, for that I have to select a Launch Configuration. But there is no main class in project A. (I recursively grepped for main and didn't find any). What can I do to export jar of project A that contains respective source code also (which will be used in line-by-line debugging)?

    Read the article

  • Best way to reuse a Runnable

    - by Gandalf
    I have a class that implements Runnable and am currently using an Executor as my thread pool to run tasks (indexing documents into Lucene). executor.execute(new LuceneDocIndexer(doc, writer)); My issue is that my Runnable class creates many Lucene Field objects and I would rather reuse them then create new ones every call. What's the best way to reuse these objects (Field objects are not thread safe so I cannot simple make them static) - should I create my own ThreadFactory? I notice that after a while the program starts to degrade drastically and the only thing I can think of is it's GC overhead. I am currently trying to profile the project to be sure this is even an issue - but for now lets just assume it is.

    Read the article

  • Java: "implements Runnable" vs. "extends Thread"

    - by goosefraba19
    From what time I've spent with threads in Java, I've found these two ways to write threads. public class ThreadA implements Runnable { public void run() { //Code } } //with a "new Thread(threadA).start()" call public class ThreadB extends Thread { public ThreadB() { super("ThreadB"); } public void run() { //Code } } //with a "threadB.start()" call Is there any significant difference in these two blocks of code?

    Read the article

  • Java: "implements Runnable" vs. "extends Thread"

    - by user65374
    From what time I've spent with threads in Java, I've found these two ways to write threads. public class ThreadA implements Runnable { public void run() { //Code } } //with a "new Thread(threadA).start()" call public class ThreadB extends Thread { public ThreadB() { super("ThreadB"); } public void run() { //Code } } //with a "threadB.start()" call Is there any significant difference in these two blocks of code?

    Read the article

  • How to compile runnable jar from packages?

    - by sacamano
    Hey. My java application has got a package structure similar to this; src/com/name/app , src/com/name/app/do , src/com/name/utils/db and so on. How would I go about compiling java files in these directories in to a runnable jar? I need to package required libraries into the generated JAR (jdbc). I've always done these things in Eclipse but now I need to supply a couple of people with a way to compile the repository without the use of eclipse and I was thinking of making a makefile or a script that invokes the necessary javac pattern. Any help is greatly appreciated.

    Read the article

  • how to implement a callback in runnable to update other swing class

    - by wizztjh
    I have a thread like this public class SMS { class Read implements Runnable { Read(){ Thread th = new Thread(this); th.start(); } @Override public void run() { // TODO Auto-generated method stub while (true){ Variant SMSAPIReturnValue = SMSAPIJava.invoke("ReadSMS"); if (SMSAPIReturnValue.getBoolean()){ String InNumber = SMSAPIJava.getPropertyAsString("MN"); String InMessage = SMSAPIJava.getPropertyAsString("MSG"); } } } } } How do I update the message to another GUI class in the same package(I understand how to put nested class to another package ....). Should I implement a callback function in SMS class? But how? Or should I pass in the Jlabel into the class?

    Read the article

  • Java: Can't implement runnable on a test case: void run() collides

    - by Zombies
    So I have a test case that I want to make into a thread. I cannot extend Thread nor can I implement runnable since TestCase already has a method void run(). The compilation error I am getting is Error(62,17): method run() in class com.util.SeleneseTestCase cannot override method run() in class junit.framework.TestCase with different return type, was class junit.framework.TestResult. What I am trying to do is to scale a Selenium testcase up to perform stress testing. I am not able to use selenium grid/pushtotest.com/amazon cloud at this time (installation issues/install time/resource issues). So this really is more of a Java language issue for me.

    Read the article

  • Java ME Runnable object takes up memory although not made an instance yet

    - by user1646684
    I am facing a strange problem with memory in Java ME. here is a part of my code: int variable=1; while (true) { if (variable==2) { display = Display.getDisplay(this); MyCanvas mc = new MyCanvas(this); // MyCanvas is a runnable object mcT = new Thread(mc); // new thread for MyCanvas mc.repaint(); display.setCurrent(mc); mcT.start(); // run thread } if (variable==1) { // Do some other stuff } } The problem is that although still the variable is set to 1, so it does not come through the if (variable==2) condition the program consumes 300kB more memory than when I delete the code after condition if (variable==2). As far as I know the code should by executed and the objects shall be created only when I set variable to value 2. But it consumes the memory also when the code after condition "if (variable==2)" is not executed. Why does this happen?

    Read the article

  • Java problem with multiple threads when executing a runnable jar file

    - by Spi1988
    I have developed a Java Swing application, which uses the SwingWorker class to perform some long running tasks. When the application is run from the IDE (Netbeans), I can start multiple long running tasks simultaneously without any problem. I created a runnable jar file for the application, in order to be able to run it from outside the IDE. The application when run from this jar file works well with the only exception that it doesn't allow me to start 2 long running tasks simultaneously. When I start the first task (assume it takes 2 minitues to complete), every thing works fine, the UI does not freeze (it never freezes). However, when I try to run another task (assume it takes just 10 seconds, therefore it should finish before the first task) while the first task has not yet completed, nothing seems to happen. In reality, the second task would have started, and also finished its processing, however its results are only displayed once the first task completes. I dunno why this is happening. Is there some restriction on the number of threads which could run simultaneously on the JVM? Are there any jvm arguments which i could try to solve this problem. I hope i explained my problem well. Thanks in advance, Peter Bartolo

    Read the article

  • Creating UTF-8 files in Java from a runnable Jar

    - by RuntimeError
    I have a little Java project where I've set the properties of the class files to UTF-8 (I use a lot of foreign characters not found on the default CP1252). The goal is to create a text file (in Windows) containing a list of items. When running the class files from Eclipse itself (hitting Ctrl+F11) it creates the file flawlessly and opening it in another editor (I'm using Notepad++) I can see the characters as I wanted. +--------------------------------------------------+ ¦ Universidade2010 (18/18)¦ ¦ hidden: 0¦ +--------------------------------------------------¦ But, when I export the project (using Eclipse) as a runnable Jar and run it using 'javaw -jar project.jar' the new file created is a mess of question marks ???????????????????????????????????????????????????? ? Universidade2010 (19/19)? ? hidden: 0? ???????????????????????????????????????????????????? I've followed some tips on how to use UTF-8 (which seems to be broken by default on Java) to try to correct this so now I'm using Writer w = new OutputStreamWriter(fos, "UTF-8"); and writing the BOM header to the file like in this question already answered but still without luck when exporting to Jar Am I missing some property or command-line command so Java knows I want to create UTF-8 files by default ?

    Read the article

  • Pourquoi réinventer la roue quand il y a Runnable ? La startup ambitionne de devenir le « YouTube du Code »

    Pourquoi réinventer la roue quand il y a Runnable ? La startup ambitionne de devenir le « YouTube du Code » Runnable, qui a récemment été lancé par une startup du même nom basée à Palo Alto avec pour objectif la facilitation de la découverte et de la réutilisation de portions de code, a annoncé qu'elle a soulevé une levée de fonds de 2 millions de dollars grâce à la participation de Sierra Ventures, Resolute VC, AngelPad et 500 startups.Yash Kumar Directeur Général et co-fondateur de la start-up...

    Read the article

  • Android - Question on postDelayed and Threads

    - by Chris
    I have a question about postDelayed. The android docs say that it adds the runnable to the queue and it runs in the UI thread. What does this mean? So, for example, the same thread I use to create my layout is used to run the Runnable? What if I want it as an independent thread that executes while I am creating my layout and defining my activity? Thanks Chris

    Read the article

  • Sockets, Threads and Services in android, how to make them work together ?

    - by Spredzy
    Hi all, I am facing a probleme with threads and sockets I cant figure it out, if someone can help me please i would really appreciate. There are the facts : I have a service class NetworkService, inside this class I have a Socket attribute. I would like it be at the state of connected for the whole lifecycle of the service. To connect the socket I do it in a thread, so if the server has to timeout, it would not block my UI thread. Problem is, into the thread where I connect my socket everything is fine, it is connected and I can talk to my server, once this thread is over and I try to reuse the socket, in another thread, I have the error message Socket is not connected. Questions are : - Is the socket automatically disconnected at the end of the thread? - Is their anyway we can pass back a value from a called thread to the caller ? Thanks a lot, Here is my code public class NetworkService extends Service { private Socket mSocket = new Socket(); private void _connectSocket(String addr, int port) { Runnable connect = new connectSocket(this.mSocket, addr, port); new Thread(connect).start(); } private void _authentification() { Runnable auth = new authentification(); new Thread(auth).start(); } private INetwork.Stub mBinder = new INetwork.Stub() { @Override public int doConnect(String addr, int port) throws RemoteException { _connectSocket(addr, port); _authentification(); return 0; } }; class connectSocket implements Runnable { String addrSocket; int portSocket; int TIMEOUT=5000; public connectSocket(String addr, int port) { addrSocket = addr; portSocket = port; } @Override public void run() { SocketAddress socketAddress = new InetSocketAddress(addrSocket, portSocket); try { mSocket.connect(socketAddress, TIMEOUT); PrintWriter out = new PrintWriter(mSocket.getOutputStream(), true); out.println("test42"); Log.i("connectSocket()", "Connection Succesful"); } catch (IOException e) { Log.e("connectSocket()", e.getMessage()); e.printStackTrace(); } } } class authentification implements Runnable { private String constructFirstConnectQuery() { String query = "toto"; return query; } @Override public void run() { BufferedReader in; PrintWriter out; String line = ""; try { in = new BufferedReader(new InputStreamReader(mSocket.getInputStream())); out = new PrintWriter(mSocket.getOutputStream(), true); out.println(constructFirstConnectQuery()); while (mSocket.isConnected()) { line = in.readLine(); Log.e("LINE", "[Current]- " + line); } } catch (IOException e) {e.printStackTrace();} } }

    Read the article

  • iPhone : is javaScript runnable in UIWebView ?

    - by yakub_moriss
    Hi, Experts i want to change image periodically in UIImageView in my Application. and image will come from web. can i run javaScript enable page in UIWebView ? is it possible to do ? or any other way to implement this ? any code snippet if awailable... waiting for you reply... thanks in advance.

    Read the article

  • Android Progress Dialog not showing

    - by ilomambo
    This is the handler, in the main Thread, which shows and dismisses a progress dialog. public static final int SHOW = 0; public static final int DISMISS = 1; public Handler pdHandler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(TAG, "+ handleMessage(msg:" + msg + ")"); switch(msg.what) { case SHOW: pd = ProgressDialog.show(LogViewer.this, "", getText(R.string.loading_msg), true); break; case DISMISS: if(pd != null) { pd.dismiss(); pd = null; } break; } } }; The message to show the progress is: pdHandler.sendMessage(pdHandler.obtainMessage(SHOW)); The message to dismiss it is: pdHandler.sendMessage(pdHandler.obtainMessage(DISMISS)); It works well when I call it before I start an AsyncTask, the AsyncTask onPostExecute() dismisses it. But when it runs within a runnable (runOnUiThread), the dialog does not show. Examining the pd variable on the debugger, it shows that is is created, it is running and it is visible, but in practice it is not visible. Any suggestion? UPDATE: I did the obvious test I should have done in the first place. I commented the DISMISS message. And the progress dialog did show up. It appeared too late, after the runnable was finished. I undestand now that the DISMISS message did dismiss the not-yet-visible ProgressDialog, that's why I did not see it. The question becomes now: I need the ProgressDialog to show BEFORE the runnable code is executed. And it is not so straight forward. My call hierarchy is like this: onScrollEventChanged --> runOnUiThread ( --> checkScrollLimits --> if need to scroll show ProgressDialog "Loading" get new data into table dismiss ProgressDIalog ) I tried something like this: onScrollEventChanged --> checkScrollLimits --> if need to scroll show ProgressDialog "Loading" --> runOnUiThread ( get new data into table dismiss ProgressDIalog ) But still the dismiss message got there before the ProgressDialog could show. According to Logcat there is a five second interval between the arrival of the SHOW message and the arrival of the DISMISS message. UPDATE II: I though I will use the isShowing() method of ProgressDIalog pd = ProgressDialog.show(...) while(!pd.isShowing()); But it does not help at all, it returns true even if the dialog is not showing yet.

    Read the article

  • how to restart a Thread?

    - by wizztjh
    It is a RMI Server object , so many sethumanActivity() might be run , how do i make sure the previous changeToFalse thread will be stop or halt before the new changeToFalse run? t. interrupt ? Basically when sethumanActivity() is invoke , the humanActivity will be set to true , but a thread will be run to set it back to false. But I am thinking for how to disable or kill the thread when another sethumanActivity() invoked? public class VitaminDEngine implements VitaminD { public boolean humanActivity = false; changeToFalse cf = new changeToFalse(); Thread t = new Thread(cf); private class changeToFalse implements Runnable{ @Override public void run() { try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } humanActivity = false; } } @Override public void sethumanActivity() throws RemoteException { // TODO Auto-generated method stub humanActivity = true; t.start(); } public boolean gethumanActivity() throws RemoteException { // TODO Auto-generated method stub return humanActivity; } } Edited after the help of SOer package smartOfficeJava; import java.rmi.RemoteException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class VitaminDEngine implements VitaminD { public volatile boolean humanActivity = false; changeToFalse cf = new changeToFalse(); ExecutorService service = Executors.newSingleThreadExecutor(); private class changeToFalse implements Runnable{ @Override public void run() { try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } humanActivity = false; } } @Override public synchronized void sethumanActivity() throws RemoteException { humanActivity = true; service.submit(cf); } public synchronized boolean gethumanActivity() throws RemoteException { return humanActivity; } }

    Read the article

  • Android Signal 11 (SIGSEGV)

    - by Naturjoghurt
    I read many posts here and on other sites, but can not find the problem creating my Error: I use an AsyncTask because I want to easily manipulate the UI Thread before and after Execution. In doInBackground I create a ThreadPoolExecutor and execute Runnables. If I only execute 1 Runnable with the Executor, there is no Problem, but if I execute another Runnable I get following Error: 06-26 18:00:42.288: A/libc(25073): Fatal signal 11 (SIGSEGV) at 0x7f486162 (code=1), thread 25106 (pool-1-thread-2) 06-26 18:00:42.304: D/dalvikvm(25073): GC_CONCURRENT freed 119K, 2% free 8908K/9056K, paused 4ms+4ms, total 45ms 06-26 18:00:42.327: I/System.out(25073): In Check All with Prefix: a and Length: 4 06-26 18:00:42.390: I/DEBUG(126): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 06-26 18:00:42.390: I/DEBUG(126): Build fingerprint: 'google/yakju/maguro:4.2.2/JDQ39/573038:user/release-keys' 06-26 18:00:42.390: I/DEBUG(126): Revision: '9' 06-26 18:00:42.390: I/DEBUG(126): pid: 25073, tid: 25106, name: pool-1-thread-2 >>> de.uni_duesseldorf.cn.distributed_computing2 <<< 06-26 18:00:42.390: I/DEBUG(126): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 7f486162 ... 06-26 18:00:42.538: I/DEBUG(126): memory map around fault addr 7f486162: 06-26 18:00:42.538: I/DEBUG(126): 60292000-60391000 06-26 18:00:42.538: I/DEBUG(126): (no map for address) 06-26 18:00:42.538: I/DEBUG(126): bed14000-bed35000 [stack] I set up the ThreadPoolExecutor like this: // numberOfPackages: Number of Runnables to be executed public void initializeThreadPoolExecutor (int numberOfPackages) { int corePoolSize = Runtime.getRuntime().availableProcessors(); int maxPoolSize = numberOfPackages; long keepAliveTime = 60; final BlockingQueue workingQueue = new LinkedBlockingQueue(); executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, workingQueue); } I have no clue, why it fails when starting the second Thread. Maybe Memory Leaks? Any Help appreciated. Thanks in Advance

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >