Week 12 - Threads and Network Programming Flashcards

1
Q

What is multithreading?

A

Multithreading is the ability of executing multiple different paths of code at the same time..
Normally, your Java code is using a single thread, meaning that everything you do is synchronized, however, you can run multiple threads that will complete their respective tasks independently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What 2 ways are there to use threads in Java?

A

By extending the Thread class and by implementing the runnable interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many tasks can a computer execute at the same time?

A

Number of cores it has.

If it has only one core or if all of the cores are busy, it switches between the tasks very fast, and you get the illusion that it executes the tasks concurrently while it is not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does yield() do?

A

If the thread calls the yield() method, the scheduler checks if there is any thread on the ready state waiting to be executed, it moves the current thread to the Ready state and gives the processor another task.
yield()
method is called on the Thread object and accepts no parameter and does not return any output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is one advantage using Runnable interface over Thread class?

has to do with “class” and “interface”

A

Once we extend one class we cannot extend any other, while if we can implement as many interfaces as we would like.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does .join() do?

A

The join() method causes the current thread (the one on which join() is called) to wait until the thread it is called on (the thread being joined) completes its execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to calculate the time needed for specific task to be executed?

A

long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does syncronization ensure?

A

Synchronization ensures that only one thread can access the shared resource at a time.
Without it, the outcome would’ve been different for each run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is ExecutorService thread pool?

A

ExecutorService thread pool is a mechanism provided by the java.util.concurrent package to manage and reuse a pool of worker threads for executing tasks concurrently. Instead of creating a new thread for each task, which can be inefficient due to the overhead of thread creation and destruction, an ExecutorService maintains a pool of threads that can be reused for multiple tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do URLs consist of?

A

Scheme: protocol (http, https, ftp)
Domain: (www.example.com)
Path: location of a specific resource (/page1)
Query parameters: used to pass information to the server. Separated from the rest by question mark (?)
Fragment Identifier: points to a specific section within the resource. Separated from the rest by a hash (#)

https://www.example.com/path/page?name=value#section

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is XML?

A

XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.

Nowadays, JSON has become more popular.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is JSON?

A

JSON or JavaScript Object Notation, is a lightweigt data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
It is a text format that is often used to transmit data between a server and a web application, as an alternative to XML.

Used for configurations files, data storage and communication.

Data is represented in name/value pairs. Data objects are enclosed in curly braces {}. Each data member is separated by a comma. Arrays are ordered lists and are enclosed in square brackets [].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is HTTP?

A

HTTP, or Hypertext Transfer Protocol, is an application-layer protocol that facilitates communication between web browsers and web servers.
It is the foundation of data communication on the World Wide Web and is used for transferring hypertext, which typically includes HTML documents.
HTTP defines several methods (or verbs) that indicate the desired action to be performed on a resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Most commonly used HTTP methods?

A

GET: Requests data from a specified resource.
POST: Submits data to be processed to a specified resource. It can result in the creation of a new resource or an update of existing resources on the server.
PUT: Updates a specified resource
DELETE: Requests the removal of a specified resource.
PATCH: Applies partial modifications to a resource.
HEAD: Requests the headers of a specified resource without the actual data, useful for checking the status of a resource.
OPTIONS: Requests information about the communication options available for a specified resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which class in Java provides a set of methods to send and receive data via HTTP?

A

HttpURLConnection (a subclass or URLConnection)

part of java.net.package, used for performing HTTP operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which method enables output streams for writing data to the server?

A

setDoOutput

then we take string input and convert it to the byte array

17
Q

What are sockets?

A

Sockets are a programming abstraction used for interprocess communication over a network.
They provide a standard mechanism for processes on different devices to communicate with each other.
Sockets can be thought of as the endpoints in a communication channel, and they enable data exchange between applications running on different machines.

18
Q

What are the two main types of sockets?

A

ServerSocket: This class implements a socket that listens for incoming connections from clients. Once a connection is accepted, a new socket is created for communication with the client.
Socket: This class represents a client-side socket that can connect to a server’s socket. It can be used to send and receive data with the server.

again, java.net.package