Created
May 24, 2021 00:20
-
-
Save r4lly99/64dedcc8e85fe0576388fa78e8cc3869 to your computer and use it in GitHub Desktop.
Java thread question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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