answersLogoWhite

0


Best Answer
The cleanest way to make a method thread-safe is add to the method declaration the synchronized keyword, like this:

public class MyClass{
// More code here

public synchronized void safeMethod(){
//Do something here, now is safe
}

}

The above code is the same as:

public class MyClass{
// More code here

public void safeMethod(){
synchronized (this){
//Do something here, now is safe
}
}

}
User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What makes a java method code thread-safe?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the task of main method in a java program?

The main method is simply an entry point to your executable code. When you run a Java program, it looks for a main method as the first section of code to execute, which is why all of your programs start there.


What is meant by native method in java?

The Java native method is a great way to gain and merge the power of C or C++ programming into Java. To use Java as a scientific and high performance language, when efficient native Java compilers are not fully implemented, use native method can boost the performance to at least the speed of C compiled code.


What is method call in java?

Calling a method in Java is when you run code associated with a specific class, using the name of an instance object of a class, followed by the dot operator, followed by the name of the method, followed by the arguments of the method, enclosed in parentheses.


What is the difference between method and function in java?

A function is a piece of code that can be reused by calling its name while a method is a function that is associated with a class. In Java, functions are usually referred to as static methods.


How many values can a method return in Java?

A method in java can declare only one return value and type at a time. For ex: a single method cannot have a code that returns a string in some cases and an integer in other cases. Java compiler does not let you do that. You can only have one return type for every method in java.


How do you call a method in Java?

It's when you take a object and run some code in the class of that object.


What is overridnig method and overlording method in java?

There is no such thing as overlording in Java.


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.


What does the RMI mean?

RMI means Remote Method Invocation and it is a way to programm distributed code in Java


What is the task of the main method in java platform?

It is the method that gets called when a Java application is started.


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


What is function in java terminology?

In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.