Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:12:56+00:00 2026-05-30T07:12:56+00:00

I am making a remote application in java The UDP Server Thread without ds.close()

  • 0

I am making a remote application in java

  1. The UDP Server Thread without ds.close() method

    public class UDPThreadServer extends Thread { 
    
    private DatagramSocket datagramSocket; 
    private DatagramPacket datagramPacket; 
    private byte[] data; // the array of bytes which will store data here
    
    
    
    public UDPThreadServer() {
    
    
    
        // initialize variables
        // setup something
    }
    
    public void run() { // overriding run method
    
        while(true) { // or while(!ds.isClosed())
    
            datagramSocket.receive(dp); // receive data and assign to byte array
    
    
        }
    }
    
    }
    
  2. The Main Program

    public class Main {
    
    public static UDPThreadServer udpThread; // the udp server running in thread
    
    public static void main(String[] args) {
    
    
       udpThread = new UDPThreadServer();
    
       udpThread.start(); // start the thread
    
       System.in.read();
    
    }
    
    
    }
    

Let’s say I have this method

public static void newThread() {

     udpThread = new UDPThreadServer(); // destroy and reinitialize thread  without calling    udpThread.stopUDP();

}

I am curious about Thread Safety in java.

Is it safe to call newThread() method???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:12:57+00:00Added an answer on May 30, 2026 at 7:12 am

    It depends upon what you do inside the constructor. Your code doesn’t start a new thread.

    If Code that is safe to be called by multiple threads simultaneously then it is thread safe.

    Local Variables

    Local variables are stored in each thread’s own stack. That means that local variables are never shared between threads.
    And hence they are always thread safe.

    Local Object References

    Local references to objects are a bit different.
    The reference itself is not shared. The object referenced however, is not stored in each thread’s local stack.

    All objects are stored in the shared heap. If an object created locally never escapes the method it was created in, it is thread safe.

    Here is an example of a thread safe local object:

    public void someMethod(){
    
      LocalObject localObject = new LocalObject();
    
      localObject.callMethod();
      method2(localObject);
    }
    
    public void method2(LocalObject localObject){
      localObject.setValue("value");
    } 
    

    Object Members (fields)

    Object members (member variables) are stored on the heap along with the object.
    Therefore, if two threads call a method on the same object instance and this method updates object members, the method is not thread safe.

    Fields are made safe using any of the below conditions

    1. Make it volatile
    2. Make it final (and it is accessed after the constructor has completed). more here
    3. The field access is protected by means of a lock. (Using synchronized or an explicit lock)
    4. Using Immutability
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im making a file manager (Server-Client application) so i can mess with remote computer
Iam getting OutOfMemoryException while making remote method call. RemoteEntity.SetLocalStore(DATASET); passed value is dataset. Note
I am making an application that connects to remote mysql database into some cpanel
I am making an application in which I have to store data from remote
In my iPhone application I get json data from a remote server, parse it
I am making a forum posting web application using Java and I want to
Not sure whether I am missing something. When making an actor remote, the main
I'm making simple C# Winform application. There is a Form having a textbox. I
I'm coding an application that requires remote binding, i.e., bind a remote object to
I am making a Java program, and one way to load files is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.