Skip to content

Instantly share code, notes, and snippets.

@r4lly99
Created May 24, 2021 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r4lly99/64dedcc8e85fe0576388fa78e8cc3869 to your computer and use it in GitHub Desktop.
Save r4lly99/64dedcc8e85fe0576388fa78e8cc3869 to your computer and use it in GitHub Desktop.
Java thread question
class A
{
synchronized void m1()
{
System.out.println("In m1 A");
}
void m2()
{
System.out.println("In m2 A");
}
}
// There are two threads T1 and T2.
// T1 is accessing m1 method. Will T2 be able to access m2 method on the same instance at the same time?
import java.util.HashSet;
public class Customer {
String name;
int age;
Customer(String name,int age)
{
this.name=name;
this.age=age;
}
public static void main(String[] args) {
Customer c1= new Customer("John",20);
Customer c2= new Customer("John",20);
HashSet<Customer> customerSet=new HashSet<>();
customerSet.add(c1);
customerSet.add(c2);
System.out.println(customerSet.size());
}
// getters and setters
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class IterateMapMain {
public static void main(String args[])
{
// HashMap with Country as key and capital as value
HashMap<String,String> map=new HashMap<String,String>();
map.put("India","Delhi");
map.put("Japan","Tokyo");
map.put("France","Paris");
map.put("Malaysia","Kuala Lumpur");
// Iterating java iterator
System.out.println("Iterating java Iterator");
Iterator<String> countryKeySetIterator=map.keySet().iterator();
while(countryKeySetIterator.hasNext()){
String countryKey=countryKeySetIterator.next();
map.put("Indonesia", "Jakarta");
System.out.println(countryKey);
}
System.out.println("-----------------------------");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment