I'm trying to write rules for detecting some errors in annotated multi-threaded java programs. As a toy example, I'd like to detect if any method annotated with @ThreadSafe calls a ... |
I've encountered a very strange problem.
My program looks like this:
class Outter{
class Inner extends Thread {
...
|
Lets say I have a class like this in Java:
public class Function {
public static int foo(int n) {
return n+1;
...
|
I help maintain and build on a fairly large Swing GUI, with a lot of complex interaction. Often I find myself fixing bugs that are the result of things getting ... |
I'm part of a team designing the server for a client/server model naval warfare video game (University course). We have a fairly concrete (well, I think) system design, but there is ... |
I have a static c++ library which I am writing a java interface with swig.
Library includes an analytic function that I need to call for many different inputs. Though the ... |
How does initializing an object reference from a static initializer guarantee safe publication?
|
|
This is my first foray into multi threaded land and I'm currently implementing a solution using the Java concurrency library. The code essentially takes in a number of service requests, ... |
If I have static class, how does jvm guarantee it is initialized once? What happens when two threads simultaneously try to access this first time? Is this feature language invariant?
EDIT ... |
I have to take one input from console in first java program. That input i have to pass in second java program which is getting executed as thread from the main ... |
I'm trying to write a very simple program that creates several threads to send concurrent requests to a particular URL. I measure and store response times. The problem I’m having is ... |
Is static variables are Thread specific, means
class A {
public static int i = 10;
}
Class B {
A.i = 20;
}
Class C {
...
|
I have a web application running on Tomcat.
There are several calculations that need to be done on multiple places in the web application. can i make those calculations static helper functions? ... |
I have a very simple program with a Threads. Suppose I want that the thread starts after some code (i.e. in the middle of program). How do I achieve this?
When I ... |
I would like to create a worker thread that should be shared within other sessions. Basically I want to restrict other users from doing the same process. So They will retrieve ... |
How can i get/set the "goal" value?
- So that i can also use it from other class or threads? I tried this but its always giving null or nothing instead of ...
|
I am going through Java Memory Model video presentation and author is saying it is better to use Static Lazy Initialization compared to Lazy Initialization and I do not clear understand ... |
This builds up on my previous question.
My ftp server has 10 files, say test1.txt, test2.txt and so on. I want to be able to download multiple files (max 3) ... |
I must use a numeric library whose code is generated from FORTRAN. Resulting code is very fast, but it is made of class with only static methods. The problem is that ... |
Under what situations would you ever need static threads ?
What is the significance of using a static thread ? Is it considered a good practice ?
|
Can someone explain me the difference between Class.forName() and Thread.currentThread().getContextClassLoader().loadClass().
I have next code
<code>
public class Test {
static {
System.out.println("Hello ...
|
If a class has a static method, and that method is called by two independent threads, will the the two calls be processed concurrently? My question arrises from performance considerations. If the JVM blocks one thread while the other runs then it would be better to use instance methods so blocking does not occur. Furthermore, depending on the answer to the ... |
There's nothing special about static methods, with regard to thread safety. If they access shared objects, then such access will usually need to be controlled by synchronisation; this might be on the object being accessed, some higher-level object or a special object allocated purely as a lock. The only way that you might think of them as special is that there's ... |
Hi, i am runnning a few threads, each one counts from 1-100,101-200,201-300 etc. each thread looks for prime numbers in their respective range. When one is found, it updates a static int countPrime. I then want to display prime. If I put it in main, it displays before any threads have run and is there for 0. where and how should ... |
Hi All, We have a class named Format with STATIC methods which is being used for conversion of variables from one datatype to another. For e.g: getInteger(String str) - which gives me an Integer object for String. Is there a possibility that if multiple objects access the same method concurrently than is there any possibility of one object getting Integer equivalent ... |
|
Hi, Welcome to JavaRanch... I think. I'm not sure I'm too thrilled with the sound of your display name; given that it doesn't match your signature, I suspect it's not a real name at all. You might want to have a look at our naming policy and then make some adjustments here. Now, regarding your problem: the answer is "it depends." ... |
Hello all - this is my first post , I am excited. Anyway down to business - I want to understand how static obj works in relation to threads. Here is my class that implements runnable. I am trying to synchronize on the static int[]. The issue I am having is based on my output it seems like my threads are ... |
This is question on parallel execution two different method which are static and synchronised in the same class class Test { public synchronized static One() { System.out.println("one"); } public synchronized static two() { System.out.println("two"); } } This is my understanding can this be validated. Say now multiple thread are accessing the two methods of this class. Consider that thread1 is now ... |
Hi, I would like to get better understanding of how JVM loads classes. If for example I instantiate class A in two separate threads that run concurrently. Lets say that class A creates static instance of Hashtable. Will JVM guarantee that both threads will see the same Hashtable or this implementation is broken? class A{ private static hash = new Hashtable(); ... |
Hi, I have a requirement to restrict the number of active threads. I have 3 classes, say A, B & C. B implements Runnable. C has a class level static variable, say, 'count'. 'A' has the main method. When 'A' starts, it spawns threads of B, lets say 2 in number. B increments the value of the variable 'count'. After this, ... |
Friends, I have a basic doubt which made me to confuse a lot . I have an application which will support multi threading. From the thread i am accessing a static method in a class, (for eg: Utils.getCurrentDate() where getCurrentDate() is a static method in my Utils class). Now, when i have some five to ten threads running, will there be ... |
Hi, I am trying to use a static vairable in my thread class to start and stop the thread by setting the variable true/false, but this does not seems to work. I am attaching the code below: //Thread Class class MyThread extends Thread { volatile static boolean status = false; long duration; MyThread() { this.duration = 1000; } MyThread(long duration) { ... |
currentThread(), sleep() etc contain native method calls to perform their respective functions. That's y there are static (they dont access any instance variable or methods). Iam not sure I understand ur second question, >Why a static method can access an object of its own class ?? well, why not, if the class has a stic variable of same type as the ... |
I would like to change a process to multiple thread. There is a static singleton method used in each process. There are multiple sub class of process runs. How could I change the static method to let the sub process use the variable id locallly in multiple thread and with small change? Thanks, the original code like: for (int id=0; id ... |
|
public static void main(...){ Thread t1=new Thread(...); t1.start(); // now we have 2 running threads: 'main' and 't1' t1.sleep(1000); // This would send the 'main' thread to sleep... it would have no effect on t1 ! // Because it actually means 'Thread.sleep(1000)'. // Which means: take the current thread (main), and put // it to sleep. } |
I have some static variables in a class. When the http request comes in it sets some values of the static variables and based on these static variables a list is created using values from the database. Each request comes in with different values. I guess when each request comes in it runs in a different thread. Is the values static ... |
Hi. Since the variable is private, only its class is allowed to modify it. If you have only one static method that manipulates this variable I'd say yes, it's thread-safe, because this variable is not accessible through other means. However, if you have other static methods manipulating this same variable, things change and you might need to handle multi-threading issues. |
We are working with a customer who has a legacy framework that uses static methods on the service class. The client code simply calls on this method to perform the service. When there is heavy concurrency in the system, and multiple users are trying to access the same method of a service, would this become a bottle-neck w.r.t performance, or Java ... |
Am accessing a static method from the run method. When i access the same method 1.with an object it goes synchronised with the thread. 2.accessing that as static method, the thread just gives a call to that method keeps executing the next line. inspite of that calledStaticMethod getting completed. Do threads vary from the way it calls a static method. and ... |
|
thread t1 and t2 can not execute the same method meth1() as this is static method.To work on this method one need to aquire a class level lock. Here it is already auquired by t1. Unless finishes the execution t2 can not work on it concurrently. However, t3 can access meth2() and t4 can acess meth3() concurrently. Reason it: t3 is ... |
hi all, as we know instance variables live on heap inside the object, but where do static variables live. if they live on stack, then different threads have different stacks, so in that case do they have different copies of static variables or is there some sharing mechanism going on. please correct me if my understanding is wrong.. |
Hi Chaps, I have a static method in a class. This static method is currently being accessed by various(huge in number) classes. Now we are planning to operate this code in a clustered environment. The code was - public static String getString(String parameter1, String parameter2) { // Process a logic thats specific to the parameters passed in return "processed value"; } ... |
Hi techie's, I have built a code, as such We have four java files : thread2.java thread1.java SyncThread.java threadController.java exceuting each file in seperate command prompt. thread1 and thread2 are files and each has a run method. from the run method am calling the SyncMethod - synchronized method in a while loop at file SyncThread, where it just prints the values ... |
|
Class A{ int a=1; int a=2; int a=3; int a=4; int a=5; int a=6; int a=7; int a=8; } Consider Thread -1 ,accessing the class A, for EX: at line int i=7; 1)Meanwhile another thread trying to access the class A, after the Thread 1 finished its execution, Thread 2 starts it execution ? 2)if Yes ,then in Effective java book ... |
|
How the static method handels thread safty. As per the explaination read till this time like each thread has its own stack and memory allocated to it. Even method has single copy of code How in the following code Object of stringBuffer is actually maintained by java. If there is single copy of code. When I am creating the instance of ... |
Hi All, I am writing a code which runs in a mult- threaded environment. I have declared a static variable which is globally visible across the application. Each thread landing on a piece of code has the capability to change the value of my static variable. My question is, under heavy load condition, is it possible that at the same time ... |
[color=blue] I have following doubts 1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread? 2. Can static variables in a class be searialised? 3.If a class A has two variables AB,AC A subclass of A ie B has two varaibles BC,BD and also it implements serializable interface a method is called on ... |
Hi, I need to refactor some code because I have no idea how this is supposed to work in a multi-threaded environment. So here is what's going on: - The UI is reading XMLs from a URLs. In order to avoid repetition of same code everywhere, methods like connect method (to open URLConnection and read xml string) and getDocument (to create ... |
|
I have a static map with all configuration data. When request comes to my application I read this map, retrieve some value and do further processing. I create a new thread for every request. Now If two request comes at the same time(even in miliseconds) and when I try to retrieve the value from the map, return value gets swapped. For ... |
I've a question about the Data class below around synchronization. The class in question is a Singleton and controls access to a list of records. Internally the class has a cache containing the list of records. This cache is loaded when a call is made to Data.getInstance(someFileLocation). The structure is something like this: public class Data implements DB { private static ... |
If you don't need on on demand initialisation then don't use it and do it simply. Its faster start up time vs a hit when you use it, both solutions fix the traditional singleton initialisation problem. Your question is more about on demand vs not than threading, though obviously the articles you quote are all about how to achieve this simply. ... |
Hello i am new to using threads. i have a class which calls a method of another class, but i get the non-static method can not be called from a static context, error. i have not declared anything static in the thread class. so my question is, is threads always static, and is it true that everytime i want to call ... |
Hello i am new to using threads. i have a class which calls a method of another class, but i get the non-static method can not be called from a static context, error. i have not declared anything static in the thread class. so my question is, is threads always static, and is it true that everytime i want to call ... |
I tried to write like this ( I know there is no need to creat static ssp in the first SwingUtilities.invoke..... ) but then the Second SwingUtilitlies.invoke. pops erro message saying that "ssp can't be resolved "..But since SSP has been created as object of test1 in the first SwingUtilities already..then after 1 second "TimeUnit.SECONDS.sleep(1);" when can't it resolve SSP? does ... |
The problem is that "FOUND YOU" never gets printed. I'm sure current_item gets to be 5 for at least a second so why my Checker thread isn't capturing that? I want my Checker thread running in the background constantly checking for changes to my current_item variable. Am I doing this thread thing the right way? |
The exact same rules for multithreading apply to both static and non-static methods--if the method accesses any shared variables that might be accessed by another thread, and if you need atomicity or need threads to see each others' updates of those variables, then you need to synchronize. In your particular example, there are no shared variables, so it doesn't matter. |
Normally static and instance variables are not interchangeable. This has nothing to do with threading, however, it's just a basic fact of the Java language. However it's possible to write code which uses only a single instance of a class, and updates a static variable and an instance variable in the same way. This would result in the same things happening ... |
Hello I want to ask if I use a static variable in JAVA, and threads, for example if the thread 1 come and put a static variable = 4, then all the others threads that access to that variable will see variable = 4 right? And if I don't use static the data will not be share. All the threads will ... |
Leaving aside synchronization, is your problem that the number of messages sent must be equal to the number of times that setState() is called? Then yes, you are correct, the code you have there will not do that. To make that happen with the code you have, there would have to be something that caused calls to setState() and calls to ... |
|
Hi all, I have an applet that uses static object references in its class. It runs fine for the first time, but when I close the window (but not the Browser) and restart the applet it will not work properly probably due to the still alive references. Expecting the user to close the browser every time to restart the applet is ... |