2018.1.10 AndyLee
sleep()是使線程停止一段時間的方法。在sleep 時間間隔期滿後,線程不一定立即恢復執行。這是因為在那個時刻,其他線程可能正在運行而且沒有被設定為放棄執行,除非(a)“醒來”的線程具有更高的優先順序 (b)正在運行的線程因為其他原因而阻塞。
wait()是線程交互時,如果線程對一個同步物件x 發出一個wait()調用,該線程會暫停執行,被調物件進入等待狀態,直到被喚醒或等待時間到。
Sleep()無法由其他thread喚起,wait()可以。
stack: 可被預測生命週期的變數或函數資訊都放在stack,例如區域變數(local variable)、函式參數(function/method parameter)、函數的返回位址(function/method return address)等資訊
heap: 動態配置的記憶體空間,放置被new出來的物件以及內含的成員變數及方法
一般來說stack爆掉是因為有太多區域變數或recursive function跑太深,heap爆掉則是GC沒有做好物件無法被回收
String: 字符串常量,字符串長度不可變。
StringBuffer: StringBuffer的長度是可變的
StringBuilder: 是java5.0的新對象,長度也是可變的,和stringBuffer類似。特殊之處是它不是線程安全的,好處是效率比stringBuffer要高,兩者的方法基本相同
原文網址:https://read01.com/x85KyJ.html
陣列僅有length的屬性,String有length()方法。
final是修飾的關鍵字,可修飾類別、方法 和變數。
finally 在例外處理中(try…catch….finally)最後一定會被執行的區塊。
finalize當garbage collector發現某物件已經不被任何物件所參考(reference) 會呼叫該物件的finalize(),finalize()內沒有什特別的動作一般會被OVERRIDE拿來作為資源的釋放。
兩個 : 一個是“xyx”,一個是指向“xyx”的引用物件s。
short s1 = 1;
s1 = s1 + 1;
short s1 = 1;
s1 += 1;
S1 +1會回傳int,int不能指定給short;
S1 += 1 沒錯。
List和Set是 ,Map不是
不可,abstract method不可包含private, final, static, native, synchronized等關鍵字
會在return前執行。
2<<3
Switch(exp),exp為整數運算式,故僅有int、 short、 char 或者 byte可以作用。
public: main方法是Java程序運行時調用的第一個方法,因此它必須對Java環境可見。所以可見性設置為pulic.
static: Java平台調用這個方法時不會創建這個類的一個實例,因此這個方法必須聲明為static。
void: main方法沒有返回值。
String 是命令行傳進參數的類型,args是指命令行傳進的字符串數組。
原文網址:https://kknews.cc/zh-tw/other/6e3vjl.html
System是系統提供的預定義的final類,out是一個PrintStream對象,println是out對象裡面一個重載的方法。
Both process and Thread are independent path of execution but one process can have multiple Threads.
Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and share it with other threads.
Threads are also refereed as task or light weight process (LWP) in operating system
Threads from same process can communicate with each other by using Programming language construct like wait and notify in Java and much simpler than inter process communication (IPC).
It's easy to create Thread as compared to Process which requires duplication of parent process.
All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack in Java.
start()方法被用來啟動新創建的線程,而且start()內部調用了run()方法,這和直接調用run()方法的效果不一樣。
當你調用run()方法的時候,只會是在原來的線程中調用,沒有新的線程啟動,start()方法才會啟動新線程。更多討論請點擊這裡
Java 的變數依照其存活時間長短分成下列四種 :
又稱為靜態變數, 因為它必須在類別成員區宣告為 static (也只能在類別成員區有 static), 例如 :
static int count=0;
其特性如下 :
static variables 一開始就被載入記憶體,並且放在靜態區,給了它不一樣的特性:
class VariableDemo{
static int count=0;
public void increment(){
count++;
}
public static void main(String args[]){
VariableDemo obj1=new VariableDemo();
VariableDemo obj2=new VariableDemo();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
輸出:
Obj1: count is=2
Obj2: count is=2
從上面的範例中我們可以發現,雖然是透過不同的物件中的 increment() 操作,但實際上操作的卻是同一區塊,證明 static 變數不是跟其他同 class 的元素一起放在heap區,而是獨立放在靜態區,因此 obj1 和 obj2 的 count 都是指向記憶體的同一區塊。
又稱為物件變數, 或物件的屬性, 係類別成員中沒有 static宣告之變數. 其特性如下 :
此為method中的variable以及內容中所用到的變數,是其特性如下 :
即 if, switch, for, while, do-while 等流程控制語法大括弧內所定義之變數, 是兩種區域變數之一, 其特性如下 :
Private:同一個class內才可存取
Protected:同一個package的class才可存取,不同的package得要有繼承關係才可存取
Public:皆可存取
Default (無修飾元) : 同一個package的class才可存取
Interface:
Abstract Class:
final class
)Anonymous Class: 匿名類別實際上也擁有一個.class檔案,在概念上,它其實就是將一個類別或是介面在編譯new statement時由編譯器重新產生出另一個類別
Abstract Class提供一種多個class一起合作工作的方式, 將多個class中相同的元素pull up method到public class中, 再以繼承的方式來使用它, 目的是為了實現多型精神
Ex: 數學計算包括求各種形狀的面積、體積 、數字的立方 、平方等, 就可以把類似的方法集中到同一個public class中
abstract class Shape {
String name;
double length, width, heigh;
double radius;
abstract double area(); // 求面積
abstract double perimeter(); // 求周長
// abstract class中也可以有建構方法, 但必須在子類別中被呼叫
// 四邊形的建構方法
public Shape(double length, double width, String name){
this.length = length;
this.width = width;
this.name = name;
}
// 三角形的建構方法
public Shape(String name, double width, double height){
this.height = height;
this.width = width;
this.name = name;
}
}
// 三角形
class EqualTriangle extends Shape {
public EqualTrangle(String name, double width, double height){
super(name, width, height);
}
double area(){
return (width * height) / 2 ;
}
double perimeter(){
return 3 * width;
}
}
// 長方形
class Rectangular extends Shape {
public Rectangular(double length, double width, String name){
//在子類別中呼叫父類別的建構方法用super()
super(length, width, name);
}
// 因為繼承Shape類別, 因此要實作area和perimeter的abstract class
double area(){
return length * width;
}
double perimeter(){
return (length + width) * 2;
}
}
Ref
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Comparable
and Cloneable
are implemented by many unrelated classes.An example of an abstract class in the JDK is AbstractMap
, which is part of the Collections Framework. Its subclasses (which include HashMap
, TreeMap
, and ConcurrentHashMap
) share many methods (including get
, put
, isEmpty
, containsKey
, and containsValue
) that AbstractMap
defines.
An example of a class in the JDK that implements several interfaces is HashMap
, which implements the interfaces Serializable
, Cloneable
, and Map<K, V>
. By reading this list of interfaces, you can infer that an instance of HashMap
(regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream; see the section Serializable Objects), and has the functionality of a map. In addition, the Map<K, V>
interface has been enhanced with many default methods such as merge
and forEach
that older classes that have implemented this interface do not have to define.
Note that many software libraries use both abstract classes and interfaces; the HashMap
class implements several interfaces and also extends the abstract class AbstractMap
.
In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObject
s must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject
) as shown in the following figure.
Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject
First, you declare an abstract class, GraphicObject
, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo
method.
GraphicObject
also declares abstract methods for methods, such as draw
or resize
, that need to be implemented by all subclasses but must be implemented in different ways.
The GraphicObject
class can look something like this:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
// delcare abstrct methods
abstract void draw();
abstract void resize();
}
Each nonabstract subclass of GraphicObject
, such as Circle
and Rectangle
, must provide implementations for the draw
and resize
methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
In the section on Interfaces
, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract
. For example,
abstract class X implements Y {
// implements all but one method of Y
}
class XX extends X {
// implements the remaining method in Y
}
In this case, class X
must be abstract
because it does not fully implement Y
, but class XX
does, in fact, implement Y
.
References:
重載(Overload): 表示同一個類中可以有多個名稱相同的方法,但這些方法的參數列表各不相同,即參數個數、類型或順序不同。
重寫(Override) :表示子類別中的方法可以與父類中的某個方法的名稱和參數完全相同,通過子類創建的實例對象調用這個方法時,將調用子類中的定義方法,這相當於把父類中定義的那個完全相同的方法給覆蓋了,這也是面向對象編程的多態性的一種表現。
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
}).start();
new Thread的弊端如下:
相比new Thread,Java提供的四種ThreadPool的好處在於:
Java通過ThreadPoolExecutors提供四種線程池,分別為:
Ref