0% found this document useful (0 votes)
15 views2 pages

Java Multithreading Cheat Sheet

This document provides a concise overview of Java multithreaded programming, including the creation of threads by extending the Thread class or implementing the Runnable interface. It explains synchronization to prevent race conditions and introduces Java collections such as List, Set, and Map, detailing their characteristics and methods. Additionally, it includes examples of thread creation and the use of lambda expressions.

Uploaded by

bishthardik510
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Java Multithreading Cheat Sheet

This document provides a concise overview of Java multithreaded programming, including the creation of threads by extending the Thread class or implementing the Runnable interface. It explains synchronization to prevent race conditions and introduces Java collections such as List, Set, and Map, detailing their characteristics and methods. Additionally, it includes examples of thread creation and the use of lambda expressions.

Uploaded by

bishthardik510
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Multithreaded Programming - Chit Sheet

Java Thread Model

- Thread: A lightweight sub-process; smallest unit of processing.

- Creating Threads:

* Extend Thread class:

class MyThread extends Thread {

public void run() {

[Link]("Thread running...");

new MyThread().start();

* Implement Runnable interface:

class MyRunnable implements Runnable {

public void run() {

[Link]("Runnable thread running...");

new Thread(new MyRunnable()).start();

Synchronization

- Race Condition: Multiple threads access and modify shared data.

- Use synchronized keyword:

synchronized void method() {

// thread-safe code

- Synchronized block:

synchronized(obj) {

// critical section

Runnable Interface
Java Multithreaded Programming - Chit Sheet

- Used to create threads without extending Thread class.

- Method: public void run()

- Example with lambda:

Runnable r = () -> [Link]("Using Lambda!");

new Thread(r).start();

Collections Overview

- List (ordered, allows duplicates):

* ArrayList, LinkedList

* Methods: add(), get(), remove()

- Set (unordered, no duplicates):

* HashSet, LinkedHashSet, TreeSet

* Methods: add(), contains(), remove()

- Map (key-value pairs):

* HashMap, TreeMap, LinkedHashMap

* Methods: put(), get(), remove()

You might also like