Лекция 2. «Многопоточность»
public abstract class MyAbstractClass {
private int a = 0;
public int getSum(){
return a + getB();
}
protected int getA(){...
public class MyClass extends MyAbstractClass implements MyInterface {
private int b = 1;
public int getB(){
return b;
}
pu...
public static void main(String[] args) {
//MyAbstractClass aObject = new MyAbstractClass(); - ошибка
MyClass object1 = new...
static Class<T> forName(String className)
String getCanonicalName()
Fields[] getField(String name)
Class[] getInterfaces()...
public Class<?> getClass()
public String toString()
public boolean equals(Object obj)
public int hashCode()
protected Obje...
Stack
public class GenericExample<T> {
private T value;
public GenericExample(T value){
this.value = value;
}
public T getT(){
r...
public class LongId<T> {
private long id;
public LongId(long id){
this.id = id;
}
public long getLong(){
return id;
}
}
pu...
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public ...
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static ...
try {
Thread.sleep(5000);
} catch (InterruptedException e) { // We've been interrupted.
return;
}
for (int i = 0; i < inpu...
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static ...
public class HelloThread extends Thread {
private StringBuilder builder;
public HelloThread(StringBuilder builder){
this.b...
public synchronized void increment() {
j++;
}
public void addName(String name) {
synchronized(lockObject) {
lastName = nam...
public class TwoMonitors {
private long c1 = 0;
private long c2 = 0;
private Object lock1 = new Object();
private Object l...
public void c1c2Up() {
synchronized(lock1) {
c1++;
synchronized(lock2) {
c2++;
}
}
}
public void c2c1Up() {
synchronized(l...
private boolean needDoSomething;
public void run() {
while(true){
if(needDoSomething){
doSomething();
}
Thread.sleep(1000)...
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Java осень 2012 лекция 2
Upcoming SlideShare
Loading in …5
×

Java осень 2012 лекция 2

153 views
107 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
153
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java осень 2012 лекция 2

  1. 1. Лекция 2. «Многопоточность»
  2. 2. public abstract class MyAbstractClass { private int a = 0; public int getSum(){ return a + getB(); } protected int getA(){ return a; } abstract int getB(); } public interface MyInterface { int getDif(); }
  3. 3. public class MyClass extends MyAbstractClass implements MyInterface { private int b = 1; public int getB(){ return b; } public int getDif(){ return getA() - b; } } public class MyClassChild extends MyClass implements Interface1, Interface2{ … }
  4. 4. public static void main(String[] args) { //MyAbstractClass aObject = new MyAbstractClass(); - ошибка MyClass object1 = new MyClass(); int result1 = object1.getSum(); int result2 = object1.getDif(); doSomething(object1); MyInterface object2 = new MyClass(); //int result3 = object2.getSum(); - ошибка int result4 = object2.getDif(); doSomething(object2); } public static void doSomething(MyInterface object){ //MyClass object3 = object; - ошибка //int result5 = object.getSum(); - ошибка int result6 = object.getDif(); }
  5. 5. static Class<T> forName(String className) String getCanonicalName() Fields[] getField(String name) Class[] getInterfaces() Method[] getMethods() Constructor[] getConstructors()
  6. 6. public Class<?> getClass() public String toString() public boolean equals(Object obj) public int hashCode() protected Object clone()
  7. 7. Stack
  8. 8. public class GenericExample<T> { private T value; public GenericExample(T value){ this.value = value; } public T getT(){ return value; } public static void main(String[] args) { GenericExample<Integer> intObject = new GenericExample<Integer>(1); Integer valueInteger = intObject.getT(); GenericExample<String> stringObject = new GenericExample<String>("word"); String valueString = stringObject.getT(); } }
  9. 9. public class LongId<T> { private long id; public LongId(long id){ this.id = id; } public long getLong(){ return id; } } public void manyIdsInParams(long userId, long serverId, long adress){…} public void manyIdsInParams(LongId<User> userId, LongId<Server> serverId, LongId<Adress> adress){…}
  10. 10. public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }
  11. 11. public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
  12. 12. try { Thread.sleep(5000); } catch (InterruptedException e) { // We've been interrupted. return; } for (int i = 0; i < inputs.length; i++) { heavyTask(inputs[i]); if (Thread.interrupted()) { // We've been interrupted. return; } }
  13. 13. public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { Thread thread = new HelloThread(); thread.start(); thread.join(); System.out.println("Hello from the main!"); } }
  14. 14. public class HelloThread extends Thread { private StringBuilder builder; public HelloThread(StringBuilder builder){ this.builder = builder; } public void run() { builder.append("Hello from a thread!"); } public static void main(String args[]) { StringBuilder builder = new StringBuilder(); Thread thread = new HelloThread(); thread.start(); builder.append("Hello from the main!"); } }
  15. 15. public synchronized void increment() { j++; } public void addName(String name) { synchronized(lockObject) { lastName = name; nameCount++; } nameList.add(name); }
  16. 16. public class TwoMonitors { private long c1 = 0; private long c2 = 0; private Object lock1 = new Object(); private Object lock2 = new Object(); public void c1Up() { synchronized(lock1) { c1++; } } public void c2Up() { synchronized(lock2) { c2++; } } }
  17. 17. public void c1c2Up() { synchronized(lock1) { c1++; synchronized(lock2) { c2++; } } } public void c2c1Up() { synchronized(lock2) { c2++; synchronized(lock1) { c1++; } } }
  18. 18. private boolean needDoSomething; public void run() { while(true){ if(needDoSomething){ doSomething(); } Thread.sleep(1000); } }

×