run 1 « Operation « Java Thread Q&A

Home
Java Thread Q&A
1.concurrency
2.Development
3.Exception
4.Notify
5.Operation
6.Socket
7.State
8.synchronize
9.Thread Safe
10.ThreadPool
Java Thread Q&A » Operation » run 1 

1. Why is my Java program leaking memory when I call run() on a Thread object?    stackoverflow.com

(Jeopardy-style question, I wish the answer had been online when I had this issue) Using Java 1.4, I have a method that I want to run as a thread some of the ...

2. Ways for lazy "run once" initialization in Java with override from unit tests    stackoverflow.com

I'm looking for a piece of code which behaves a bit like a singleton but isn't (because singleton's are bad :) What I'm looking for must meet these goals:

  1. Thread safe
  2. Simple (Understand ...

3. Java Threads: Specifying what should be executed in the run function    stackoverflow.com

I have a DBAdmin class that connects to the database, and then some other classes such as Article, Category and so on, that perform certain queries to a database. I am using ...

4. Can classes in java that implement runnable have methods other than run()?    stackoverflow.com

I'm trying to implement a simple class like this:

public static void main(String args[])
{
    try
    {
     myClass test = new Thread(new myClass(stuff));
 ...

5. In Java, how do you determine if a thread is running?    stackoverflow.com

How do you determine if a thread is running?

6. what does happen if I call run() method myself?    stackoverflow.com

in the main method if I write this:

Thread aThread = new Thread();
aThread.run();
what will happen???

7. in Java, I can't assign values to variables while i'm in the run() function of a thread-extended class    stackoverflow.com

This is my code. As you see in the run method, I assign values to tStart, tEnd, tAround and wTime. But when the Thread ends, they still have the default values ...

8. i want to continously run my java threads    stackoverflow.com

I am running my java application where i have used threads... i am running this application using ant command on the terminal.. But when i close my terminal or press ctrl+c,then java program ...

9. How to ensure Java threads run on different cores    stackoverflow.com

I am writing a multi-threaded application in Java in order to improve performance over the sequential version. It is a parallel version of the dynamic programming solution to the 0/1 knapsack ...

10. In Java 6, why doesn't higher priority thread not run even if lower priority thread yields?    stackoverflow.com

In the code below, I am trying to get the higher priority thread to run by yielding the lower priority thread. But it doesn't seem to work, the higher priority thread ...

11. What causes scheduled threads not to run in Java?    stackoverflow.com

I have developed a small Java application that runs two scheduled threads via a scheduled executor service. On most computers my application runs just fine. In testing however ...

12. How to force two Java threads to run on same processor/core?    stackoverflow.com

I would like a solution that doesn't include critical sections or similar synchronization alternatives. I'm looking for something similar the equivalent of Fiber (user level threads) from Windows.

13. Need help returning object in thread run method    stackoverflow.com

I have a Java class that extends Thread, it basically looks like the following:

public class HttpRequestDispatcher extends Thread {
    private String url;
    private String method; ...

14. How do I run different threads in Java?    stackoverflow.com

Hey. I'm having some problems with threads. I understand how they work, but since they all use the same method, how do I run different threads that do completely different things, but ...

15. Java: Stopping a thread that has run for too long?    stackoverflow.com

Say I've got something like this

public void run(){
    Thread behaviourThread = new Thread(abstractBehaviours[i]);
    behaviourThread.start();
}
And I want to wait until abstractBehaviours[i] run method has either finished ...

16. Final enum in Thread's run() method    stackoverflow.com

Why is the Elvis elvis definition has to be final to be used inside the Thread run() method?

 Elvis elvis = Elvis.INSTANCE; // ----> should be final Elvis elvis = Elvis.INSTANCE
 ...

17. Why in the following code the output is different when I compile or run it more than once    stackoverflow.com

class Name implements Runnable {
    public void run() {
        for (int x = 1; x <= 3; x++) {
   ...

18. Explicit call of Runnable.run    stackoverflow.com

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 ...

19. Have threads run indefinitely in a java application    stackoverflow.com

I am trying to program a game in which I have a Table class and each person sitting at the table is a separate thread. The game involves the people passing ...

20. Java multithreading easiest way to run multiple methods at once?    stackoverflow.com

I've never had to multithread before, I understand what it is at a basic level. I'm wondering what would be the simplest and most efficent way to execute three methods at ...

21. java Thread class run() method    stackoverflow.com

Thread class has run method to implement the business logic that could be executed in parallel.But I want implement different business logics in a single run method and to run simultaneously.How ...

22. Java: (Threads/Timer) Run a function everyday at say 6AM    stackoverflow.com

I have my application on Windows platform and want a java function to be executed everyday at some particular period of time. Need some guidance how to go about it. Have ...

23. Why run method defined with abstract keyword in runnable interface    stackoverflow.com

This question may be silly but i accidentally checked in java source code that run method in runnable interface is defined with abstract keyword. But according to interface definition all methods in ...

24. How to run a thread for a given time period and then return in Java?    stackoverflow.com

I want to create a thread that listens for packets for a given time period (say 30 seconds) and then returns any messages that are received whilst listening. I can do ...

25. Can two threads run two different methods at the same point of time?    stackoverflow.com

class A {
    private synchronized f() {
        ...
        ...
    }

  ...

26. is it possible to run a timer or thread in the constructor?    stackoverflow.com

is it possible to run a timer or thread in the constructor ?

27. Convert several Java methods to run as non-blocking threads?    stackoverflow.com

Is it possible to convert a number of methods (defined in an interface and implemented in a class) to run as non-blocking threads? Certainly, i can wrap each single method in ...

28. Hi I am getting IlleagalMonitorStateException when I am trying to run this program    stackoverflow.com

public class ThreadTest
{
 public static Integer i = new Integer(0);

 public static void main(String[] args) throws InterruptedException
 {
  ThreadTest threadTest = new ThreadTest();
  Runnable odd = threadTest.new Numbers(1, "thread1");
 ...

29. Java: How to run thread separately from main program/class?    stackoverflow.com

Subj. How to run thread from main class BUT separately from that class? So a bit of details: I have one program which must run a process. That process (a cmd one) ...

30. How do I run 10 threads at a time from 100 threads in Java?    stackoverflow.com

I'm using Java 6. Suppose I create 100 threads, each to complete one task. I want to run 10 threads at a time continuously. This means that if I was running thread ...

31. Problem running multiple threads in Java    stackoverflow.com

I'm having a very weird problem.I'm working on an assignment that involves building a simulation of figures moving on a 2d "chessboard". Each figure is represented by an object implementing the ...

32. Want to use one int array in Main Function from thread's run() function    stackoverflow.com

public class fraktal extends JFrame   {

 public fraktal (String args[]) {
  calc = new Calculator(632,453,raster, this);    
  }
 calc.start();


 } 
 public static void ...

33. How to cleanup thread after it run method?    stackoverflow.com

Hi I am new to Java thread programming. I want to know what should we do to the thread after end of its run method. Is it necessary to do a ...

34. Java Thread: Run method cannot throw checked exception    stackoverflow.com

In Java thread, the 'run' method cannot throw a 'checked exception'. I came across this in the Core Java (vol 1) book. Can someone please explain the reasoning behind it? ...

35. What's a good, efficient way to implement run()?    stackoverflow.com

Should it just contain a loop like

while (true) { ... }
I find it not so efficient as it consumes the CPU so much. I would like my thread to keep ...

36. Problem in executing run() mthod    stackoverflow.com

I'm using java. I'm trying to execute a thread, but the issue I'm getting is thread.start() method is getting executed, but as we know when we call the start method of ...

37. Why can't we directly call the run() method?    stackoverflow.com

If the start() method of a thread internally calls the run() method, then why don't we directly call the run() method in our code? What are the issues involved in doing ...

38. Java: privately calling 'run' in 'Runnable' class    stackoverflow.com

I don't really know Java programming practices all that well. But I have to do an academic assignment with it. I mainly get by with Google, class references, and the super ...

39. why run method is not called?    stackoverflow.com

I have 2 questions: 1. why run() is not called when I run the program 2. if run() is called, will it change the value of randomScore?

    import java.*;


public class ...

40. Does the Java Robot class run in its own thread?    stackoverflow.com

In the Robot documentation I don't see anything about Robot extending Thread. However, I am sure I have heard people say that the Robot runs in its own thread. So, ...

41. Run Method in the Thread class not being called    stackoverflow.com

I am a pretty new guy to the world of threading, have been trying to solve this problem for a week now. The run method in the Thread class is not being ...

42. Run Java Threads sequentially    stackoverflow.com

This is an interview question, can be wrong :-) How will you execute Three threads sequentially. For eg. Thread1, Thread2, Thread3. It is not possible to pass the reference of one Thread ...

43. how many threads to run in java?    stackoverflow.com

I had this brilliant idea to speed up the time needed for generating 36 files, using 36 threads!!, unfortunatelly if I start one connection(one j2ssh connection object) with 36 Threads/sessions everything ...

44. Run 100 threads in parallel and run missing threads if some previous are finished    stackoverflow.com

For example I need to always run 100 threads to do some action. I have class which called ThreadsWorker which looks for threads count and runs missing threads if some previous are ...

45. Why is run method not called?    stackoverflow.com

package threadwork;

public class WorkingWithThreads implements Runnable {

    public static void main(String[] args) {
        WorkingWithThreads wwt = new WorkingWithThreads();
    ...

46. Stopping a clip from playing in the run    stackoverflow.com

I am currently stuck at my project, currently I can play music, but I want to be able to swap song, that way I can make a difference between main menu ...

47. How to run command line within java using thread async    stackoverflow.com

I am new to java. I am tasked to write java program to run the command lines. I tested the command line under the DOS prompt since i do not have ...

48. In Java, how run differentes methods, one in each thread?    stackoverflow.com

UPDATE: I have used a mix of the answers by extraneon and Jarrod Roberson. I have currently four methods that I want to run at same time. They are four queries to ...

49. Creating Sub-Threads From a Thread in Java    stackoverflow.com

In a java program, I spawned one thread other than the main thread, and then spawned another two threads from the original thread I created(two sub threads). In all the cases ...

50. When I run a single-threaded Java program why are there are multiple threads at the OS level?    stackoverflow.com

I run a very simple, single-threaded java program. When I check the threads using command under Ubuntu

ps -eLf
it shows there are 14 threads at OS level. I expect there is only ...

51. Get all threads that run with a specified Runnable    stackoverflow.com

I have one Runnable that is used by more than one thread:

Runnable myRunnable = new MyWorker();
Thread one = new Thread(myRunnable);
Thread two = new Thread(myRunnable);
one.start();
two.start();
How can I get all threads that are ...

52. My program will not stop after run() method in main thread finishes    stackoverflow.com

I am using Java. The main thread sends data, while a worker thread listens to responses. I also have Timer in case timeout occurs. In main(), I am calling run(), which ...

53. Java - multithreaded code does not run faster on more cores    stackoverflow.com

I was just running some multithreaded code on a 4-core machine in the hopes that it would be faster than on a single-core machine. Here's the idea: I got a fixed ...

54. Accessing array which is being updated inside threads run() method    stackoverflow.com

I have a thread that updates an array every 5 minutes. It works fine however outside of the threads run() method it seems I have no access to this array making it ...

55. is there anyway to run a thread while giving it an argument?    stackoverflow.com

Can anyone tell me if there's a way to run a thread and give it an argument ? its like Giving an argument to that Runnable's run method , sth like ...

56. How can I run thread from Main method in Java application?    stackoverflow.com

I believe variables used in static main method should be also static as well. The problem is that I cannot use this in this method at all. If I remember correctly, I ...

57. What is a correct way to run multiple threads that do different jobs in java?    stackoverflow.com

I am trying to build a network related program in Java. I have previous experience with C. In C, when you run thread, you define which method you want it to ...

58. How to run main thread as a real time thread    stackoverflow.com

In real time Java one can create a real time thread and run it via the following methods:

RealtimeThread rt = new RealtimeThread(){
    public void run(){
    ...

59. Java thread run    stackoverflow.com

I have code like this

  boolean start = false;
  ThreadX a = new ThreadX();
  a.start();
  start = true;

  Class ThreadX extends Thread {

   ...

60. clojure.java.shell/sh throws RejectedExecutionException when run in a new thread    stackoverflow.com

In a new fresh leiningen project, with its core.clj containing

(defn show-cmd
  []
  (-> (shell/sh "ls")
    :out
    println))

(defn -main
  []
  (.start (Thread. ...

61. Why any class with run() method not allowed as argument to new Thread()    stackoverflow.com

If we make a class as follows :

class A 
{
   public void run() {
       // do something
   }
}
and then do new ...

62. Is it wise to catch OutofMemoryError in run() of thread?    stackoverflow.com

I suspect in my application that an outofmemoryerror is causing a a run() to exit, however because there were no logs, the error wasn't visible. What should i do in this ...

63. thread run nullpointerexception (UDP multiclient chat server)    stackoverflow.com

Anyone know why it's throwing the nullpointerexception please? Everything seems initialized as far as I know. I am attempting to have a UDP multiclient chat server in Java. This is the ...

64. Can a Java Thread be safely stopped by return-ing inside the run() method?    stackoverflow.com

I know that Thread.stop() and other functions are deprecated, and I want to stop a thread from running if a certain condition is met. Here is a simplified version of what ...

65. Why does the FirstThread always run before the SecondThread in the following code?    stackoverflow.com

public class TowThreads {
    public static class FirstThread extends Thread {
        public void run() {
       ...

66. Getting a thread to run to completion    coderanch.com

67. choose between 2 run methods    coderanch.com

Hi , i have the follwoing example : class A implements Runnable { A() {new Thread(this).start() ;} public void run() { ~~~~~~~ }//run }//A class extendA extends A { extendA() {new Thread(this).start() ; } public viod run() { ~~~~~~~ }//run }//extendA my problem is the follwoing : extendA a=new extendA() ; object a has two threads one is define inside its ...

68. infinite loop in run    coderanch.com

If run() has an infinite loop when started,then the thread doesnt start at once but goes in ReadytoRun stage, where it waits for other threads and the schedular decides when to run this thread? Am i right? Anything to be added to it? 2) Also if i put an infinite loop in main , it gives Unreachable Statement error. It did ...

70. Will muti-threading make you program run faster?    coderanch.com


Sun Certified Programmer for Java 2 Platform (SCJP)
Sun Certified Developer for Java 2 Platform (SCJD)
Sun Certified Web Component Developer for Java2 Platform, Enterprise Edition (SCWCD)
Sun Certified Business Component Developer for Java2 Platform, Enterprise Edition (SCBCD)
Sun Certified Enterprise Architect for J2EE (SCEA)
IBM Certified Enterprise Developer, WebSphere Studio V5.0

71. Calling Directly run() method    coderanch.com

class demo extends Thread { public void run(){ for(int i=0;i<10;i++) System.out.println(i); } } class test{ public static void main(string arg[]){ demo dt = new demo(); dt.run(); System.out.println("main thread output"); } } select any 1 A) Code will give compilation error on line 10, since run can not be called directly. B) Code will compile without error, but will not show any ...

72. Thred Should run even after Program terminates    coderanch.com

Your thread problem appears to be how to keep a thread running after a program exits? Well your solution is actually hidden in what main is. Main is a thread in the process. It is a DEAMON. Well once main exists, your program exits even if you have threads running right. Well the solution is there is a mehtod call in ...

73. what will happen if i run it ?    coderanch.com

74. avoid override run method    coderanch.com

Hello , suppose i have the following class : class MyThread implements Runnable { MyThread() { new Thread(this).start() ; } public void run() { ......... } the question how we can avoid any subclass from class MyThread to override run method ??? i try to make run method final , static but it doesn't work so pleeease i need help Thanks ...

75. need to run threads with different parameters    coderanch.com

Hi , I have a code similar to this : -------------------------------------------------- import java.io.*; public class SomeClass { public SomeClass() { } public static void main(String str[]){ // get the directory name as a command line argument String dirName = str[0]; String individualfileName =""; // get an array of all the files in this directory private File[] files = (new File(dirName)).listFiles(); int ...

76. Run Method vs. Extending Thread    coderanch.com

For the project I'm working on, I think it would be really nice to have a run method which would except parameters. Of course, I know that I wouldn't call the run method directly anyway, as I would use start(). So to compensate for the lack of parameter passing in the run method should I create another class that extends Thread ...

77. throwing an exception in thread.run()    coderanch.com

78. run() method and exceptions    coderanch.com

79. Stopping a Thread (where I don't have control of run) Help needed    coderanch.com

Hi, I have a main program in which I create five more threads. I am creating and starting threads like this:.. ############################################## //loop Runnable target = (Runnable) runnableObjects.get(count); Thread newThread = new Thread(target); newThread.start(); //end loop ############################################### The run method is common and implements the Runnable Interface run method. When these threads are created I show a Dialogbox with ProgressBars for ...

80. How to wait for multiple thread to finish run    coderanch.com

I need to wait for multiple threads to finish the run so as to accumulate results The situation is more easily described below class SimpleThread extends Thread { private int countDown = 5; private static int threadCount = 0; private int threadNumber = ++threadCount; public SimpleThread() { System.out.println("Making " + threadNumber); } public void run() { while(true) { System.out.println("Thread " + ...

81. run()    coderanch.com

If you want to wait for the thread to finish executing before printing "t.i", you will need to join the thread. You have a "static Thread th" in the "test" class, which you never use. Remove the "static", and assign to this variable when you do a "new Thread". That way, you can join it later.

82. getting a client to wait in run()    coderanch.com

Hello, I'm new at this, so please be easy on me I'm trying to get a client to wait for a server's response by using the following structure in the thread's run() (using Runnable). //Sending messages to server other methods using events() { //print stream to server } //Receiving messages from server //(esp. after other clients send message to server //so ...

83. How to throw exception in run method    coderanch.com

Hi, In my thread class, when i invoke a run method, i can get an exception. I want this exception to be thrown to the method which called this thread..how do i do this..When i tried to throw exception, it is not compiling ..prompting that run method can't be overridden as throws clause has difference. Anyone can help me in this! ...

84. exceptions from run method    coderanch.com

The run() method is the top level method in its thread; the Thread class itself invokes run() and if run() throws an exception, Thread prints a stack trace. You refer to the "caller", but think about it: the "caller" you're talking about calls start(), then moves on; it's not waiting around for your run() method to throw an exception. What you ...

85. new Thread(x).run(*)?!    coderanch.com

run() is just a plain, ordinary method. If you call it, it will run. This shouldn't surprise you. Nothing stops you from calling it, just like nothing stops you from calling a main() method. It's just that run(), like main(), has a special role to play, by convention. Thread.start(), on the other hand, is a magical method. If you call it, ...

86. problem with run()    coderanch.com

Hi guys, I am new to thread. I wrote : public void start() { if(runThread==null) { runThread.start(); } } public void stop() { if(runThread!=null) { runThread.stop(); } } public void run() { while (true) { System.out.println("RUN"); } } and in keylistener event, i wrote, if (running) { runThread.suspend(); running = false; } else { runThread.resume(); running = true; } it compiles ...

87. Two Threads same run method: SCJP questions    coderanch.com

Hi all, First post here. Hoping for a warmer reception than the java.sun boards which seem to be full of people writing 'that's a stupid post' posts. It's the old chestnut question, i'm afraid: same Runnable, two Threads. I have noticed this question or variations thereof are very popular on SCJP mock exam papers. Considering this class: class Test implements Runnable ...

88. run()    coderanch.com

I have just started learning about threads so apologies if this is obvious to you. From what I have read, there are two main ways of using threads a) define a class as a subclass of thread, override the run() method and call start() to start a thread (which invokes the overridden run() method) b) define a class as implementing the ...

89. How do you get a thread to run for one second?    coderanch.com

When dealing with threads you should remove phrases like exactly one second from your vernacular. Having said that you can do something like this to come close to what you want: import java.util.Timer; import java.util.TimerTask; public class TimedRunner implements Runnable { private boolean running; private long timeToLive; public TimedRunner() { this(1000L); } public TimedRunner(long timeToLive) { this.timeToLive = timeToLive; running = ...

90. invoking run() method    coderanch.com

No this is one of basic dobut most of people will have. This code also work fine. But run() is not actual threading, it is normal method call in the program. The correct way is calling t.start() in main() method, at that point only you will leave to processor to work on your thread.

91. Run a thread at every 5mins interval ??    coderanch.com

Hi, I want that a thread should run automatically after every 5 mins interval so that it can check a folder for file to upload. I have tried as per my below codes but couldn't slove my problem. Can any one pls guide me. class ThreadTest extends Thread { public void run(){ System.out.println("Test"); try{ sleep(50000); }catch(Exception e){} } }//END OF CLASS ...

92. run() method    coderanch.com

Hello, Since it is possible to overload the run() method in a Thread subclass, how would the run(String s) method be invoked from the main method? What is the correct syntax to call it? class MyThread extends Thread { public void run() { System.out.println("running myThread"); } public void run(String s) { System.out.println("String in run is " +s); } public static void ...

93. run method    coderanch.com

Hello all, My program needs to query the DataBase every couple of seconds and get the value of one paricular column from the Db. Depending on the value (result) I have a switch case statement. I am thinking about using Timer and timerTask. but my question can the run method in the timerTask return the value queried from the Db?? here ...

94. calling another thread from a threads run method    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

95. invokeLater runnable doesn't run    coderanch.com

I have a chunk of code which is interacting with a cache (threaded) where cache-notification events attempt to kick off a runnable via SwingUtils.invokeLater as below. In my case, there are eleven cache events occurring (I hit all of them with the debugger and with the print statements) but the runnable only actually runs four times. protected void notifyPostCacheListeners(final CacheEvent ev) ...

96. How to only run a few threads at a time...    coderanch.com

I frequently receive OUT of Memory errors. I expect this... My country arrayLis listed below has a size of over 200. I only want to run maye 10 threads at a time... How do I do this? I know I can make my loop 10, but I would not know when the first 10 were done to run the next 10. ...

97. Can we make five threads run in a sequence?    coderanch.com

With JDK 5 you could also queue up tasks for a thread pool with only one thread. That would let you queue any number of tasks to run in parallel with your main thread, and know that only one of them runs at a time. I can almost imagine that being a real requirement. I guess you could also start all ...

98. is using while(true) loop in run mthd of thread good idea 2 make it wait indefinatley    coderanch.com

Thats fine. boolean aVar = true; while (aVar) { // check for if there are any messages // if there are any messages in the queue, process it //or just sleep for next 10 seconds Thread.sleep(10000); } But in this case MessgaeReceiver thread should be run as a Daemon thread in the application sothat when there no non-daemon threads, this Receiver ...

99. Have other apps run, too    coderanch.com

Normally this should be the operating system's responsibility, but on some old systems, the OS really doesn't do it well (Windows 95/98/ME, for example, or a phone). If you call Thread.sleep(), the OS ought to be able to schedule other applications while yours is sleeping. You're correct that yield() would have no effect on such a system.

100. Class implementing Runnable and calling run() method    coderanch.com

hi Vinny Menon , their is no problem in calling run method using object of class which implements (or override)that run method. but their is one difference between calling that method using object and invoking run method using start method when you call that method thats simple call that means control is transfered from main method to that method and then ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.