answersLogoWhite

0


Best Answer

A Variable that is shared as well as synchronized cannot be created in Java. These two terms are mutually exclusive and a variable that is synchronized in java cannot be shared and vice versa

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you make shared and synchronized variable in java thread?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why do you use synchronized block in java?

A Synchronized block is a block of code enclosed by the {} braces that has a synchronized keyword before it. This block of code is similar to a synchronized method and only one thread can access it at a time.


What are the synchronized methods and synchronized statements in java?

Synchronized Methods are methods that have the keyword synchronized in the method signature. Synchronized statements are pieces of java code that are surrounded by brackets which have the keyword synchronized qualifying them. Both cases mean that - only one thread will be able to access the method or the statement that is synchronized


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.


How threads are synchronized in a java?

By using an Runnable and Thread object. EX: Runnable r = new Runnable() { Thread t = new Thread() { @Override public void run() { //place code for new thread here } }; @Override public void run() { t.start(); } }; r.run();


What makes a java method code thread-safe?

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

Related questions

Why do you use synchronized block in java?

A Synchronized block is a block of code enclosed by the {} braces that has a synchronized keyword before it. This block of code is similar to a synchronized method and only one thread can access it at a time.


What are the synchronized methods and synchronized statements in java?

Synchronized Methods are methods that have the keyword synchronized in the method signature. Synchronized statements are pieces of java code that are surrounded by brackets which have the keyword synchronized qualifying them. Both cases mean that - only one thread will be able to access the method or the statement that is synchronized


What is synchronize in java?

Synchronization is a feature by which only one thread can access a resource in a particular time of instance. No other thread can interrupt for that resource. In java to make our method synchronize we use synchronized keyword with that method. So Synchronization is just managing the resources among Threads. Vijai Shanker Just to add with the invent of Java 5 we have more option to write synchronized code in Java instead of using synchronized keyword we can use Lock Classes available in concurrency package.


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.


How threads are synchronized in a java?

By using an Runnable and Thread object. EX: Runnable r = new Runnable() { Thread t = new Thread() { @Override public void run() { //place code for new thread here } }; @Override public void run() { t.start(); } }; r.run();


What is volatile and why are using in java?

volatile variable is mainly used in multithreading environment. so let me explain it from that context.In a multithreading environment,for a variable which is not marked as volatile will be stored in local cache memory for each thread. Meaning each thread will have a local copy of the variable and they dont know about what value this variable is having in another thread. If a variable is marked volatile, then the updations to this variable will happen in the main memory and not in local cache


What makes a java method code thread-safe?

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


How does one get java synchronized?

The Java program needs to be synchronized. Doing so can be complicated, but can be done. The best way to synchronize would be using the website and walk through the steps.


What is native variable in java?

native is a key word used in java method. there is no variable as native in java


What is dfference between synchronization and asynchronization in java with example?

synchornisation is the process where more than one thread can be accessed by accesssing the shared object of the class.By syncornisation we can have the communication between threads.if one thread works then another thread automatically stops


What is the correct use of volatile in java?

It is used to alert the compiler that the value of a variable may be modified by more than one thread. As a result, the Java Virtual Machine is not allowed to do certain optimizations, which are problematic in such cases.


Can you declare an instance variable of class StringBuffer in Java program?

Here's how to create a StringBuffer instance.StringBuffer sb = new StringBuffer();sb.append("Add this is to the buffer");// ...Note the StringBuffer has synchronized methods so if it is only accessed in a single thread context then a StringBuilder is preferred. StringBuffer and StringBuilder both implement the Appendable and CharSequence interfaces so can be used interchangeably.