# Server

public final class **Server**
An in-process TensorFlow server, for use in distributed training.


A
`
Server
`
instance encapsulates a set of devices and a
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Session
`
target that can participate in distributed training. A server belongs to a cluster (specified by
a
`
ClusterSpec
`
), and corresponds to a particular task in a named job. The server can
communicate with any other server in the same cluster. The server will not serve any requests
until
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#start()
`
is invoked. The server will stop serving requests once
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#stop()
`
or
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#close()
`
is invoked. Be aware that
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#close()
`
method stops the server if it is
running.


**WARNING:**
A
`
Server
`
owns resources that
**must**
be explicitly freed by
invoking
`
https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#close()
`
.


Instances of a
`
Server
`
are thread-safe.


Using example:

    import org.tensorflow.Server;
     import org.tensorflow.distruntime.ClusterDef;
     import org.tensorflow.distruntime.JobDef;
     import org.tensorflow.distruntime.ServerDef;

     ClusterDef clusterDef = ClusterDef.newBuilder()
       .addJob(JobDef.newBuilder()
       .setName("worker")
       .putTasks(0, "localhost:4321")
       .build()
     ).build();

     ServerDef serverDef = ServerDef.newBuilder()
       .setCluster(clusterDef)
       .setJobName("worker")
       .setTaskIndex(0)
       .setProtocol("grpc")
     .build();

     try (Server srv = new Server(serverDef.toByteArray())) {
       srv.start();
       srv.join();
     
     }

<br />

<br />

<br />

<br />

<br />

### Public Constructors

|---|---|
|   | [Server](https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#Server(byte[])) (byte\[\] serverDef) Constructs a new instance of server. |

### Public Methods

|---|---|
| synchronized void | [close](https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#close()) () Destroy an in-process TensorFlow server, frees memory. |
| void | [join](https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#join()) () Blocks until the server has been successfully stopped. |
| synchronized void | [start](https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#start()) () Starts an in-process TensorFlow server. |
| synchronized void | [stop](https://www.tensorflow.org/versions/r2.5/api_docs/java/org/tensorflow/Server#stop()) () Stops an in-process TensorFlow server. |

### Inherited Methods

From class java.lang.Object

|---|---|
| boolean | equals (Object arg0) |
| final Class\<?\> | getClass () |
| int | hashCode () |
| final void | notify () |
| final void | notifyAll () |
| String | toString () |
| final void | wait (long arg0, int arg1) |
| final void | wait (long arg0) |
| final void | wait () |

From interface java.lang.AutoCloseable

|---|---|
| abstract void | close () |

## Public Constructors


#### public
**Server**
(byte\[\] serverDef)


Constructs a new instance of server.

##### Parameters

| serverDef | Server definition specified as a serialized [ServerDef](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/tensorflow_server.proto) protocol buffer. |
|---|---|

## Public Methods


#### public synchronized void
**close**
()


Destroy an in-process TensorFlow server, frees memory.

##### Throws

| InterruptedException |   |
|---|---|


#### public void
**join**
()


Blocks until the server has been successfully stopped.

#### public synchronized void
**start**
()


Starts an in-process TensorFlow server.

#### public synchronized void
**stop**
()


Stops an in-process TensorFlow server.