Engineering/JAVA2008/06/02 20:15


원문 : http://blog.naver.com/windziel?Redirect=Log&logNo=60048694876


자바에서 스트링을 연결하여 사용할때

String str = "aaa" + "bbb" + "ccc";

의 형태로 사용하면 안된다고 알고 있었다.

String은 불변(Immutable) 객체이기 때문에 '+'를 이용하여 문자열을 연결하게 되면 각 ""안의 String를 생성하고 이후 블록이 끝난후 GC의 대상이 되기 때문이다.

그래서 String을 연결할 때는 StringBuffer, StringBuilder 객체를 생성하고 append 메소드를 사용하여 연결한 후 StringBuffer, StringBuilder의 toString() 메소드로 스트링을 생성한 후 사용하여야 한다고 알고 있었다. 참고로 StringBuilder는 JDK 1.5에 추가된 것으로 동기화되지않은(not safe for use by multiple threads)이다. 그러다 어디선가 String을 '+'를 이용하여 연결하여도 컴파일러가 자동으로 StringBuilder를 생성해 연결하여 준다고 보았고, for 등의 루프에서 '+'를 이용하여 연결할 경우 '+'연산을 그대로 사용한다고 보았다.

그래서!!! 테스트 고고씽~~~

간단히 String을 이런저런 방법으로 생성하는 예제를 만들고 디컴파일을 하여 비교해 보았다.


<원본 Source>

str1은 이전에 생성해 놓은 a,b,c,d,e를 '+'로 연결하여 생성하고 str2는 만번의 루프를 돌면서 a를 '+'로 계속 연결하게 한다. sb는 append메소드 안에서 e,d,c,b,a를 '+'로 연결하여 생성한다.

그럼 디컴파일된 결과는??? (디컴파일에는 jad가 수고를...)

<디컴파일된 Source>

디컴파일된 소스를 보면 컴파일러가 알아서! 자동으로! StringBuilder 객체를 생성하여 다 append 해준다. JDK 1.5, 1.6 둘다 같은 결과이다. 여태 열심히 StringBuilder를 생성하고 .append....append 한 것은 다 삽질???!!!

참고로 jdk 1.4.2에서의 디컴파일 결과이다.

<P>

그냥 다 '+'를 이용하여 연결하고 있다. 결론은 1.5이상을 사용하면 그냥 String 객체에 '+'를 사용하여도 무방할 것 같다.

Posted by 비회원
Human Life/Daily Note2008/05/22 14:32
대회 관련 블로그 : http://www.sdnkorea.com/blog/544

지난 주 토요일(2008. 5. 17) JAC에 참가하기 위해 동국대에 갔다왔다. 물론 팀은 슈퍼에이스 두명(문규, 성호)에 나는 꼽사리로 ㅋㅋ -_-

아침과 점심을 모두 굶은 나는 학교 근처에 편의점이나 식당의 거의 없는 것에 경악하였다. -_-
입장을 12:30에 하여서 동국대 모교수님과, Sun에서 오신 분들의 개회사와 대회 안내가 1:30에 끝나서 대회가 시작되었다.

노트북은 1대이상 가져오라 그래놓고 즉 사람이 3명이면 3명이상 갖고 와도 된다는 뜻, 전원은 1개 밖에 제공되지 않는 점은 정말 어이가 없었다. (대회가 열렸던 전산실의 전원공급이 부족한 것을 대회 개최측도 나중에야 안 사실이라고는 하지만 말이다.)

그리고 인터넷은 안되지만 책, 노트북에 저장되어 있는 레퍼런스, 소스코드를 모두 참조할 수 있었다. 이것도 좀 어이없었다. 노트북에 각종 알고리즘 구현소스를 담아올 수도 있는데 말이다. 물론 대회 규칙이 그런지라 나도 2~3학년 코딩했던 소스들을 마구마구 뒤지기도하였다. -_-

문제는 대체적으로 ACM보다는 쉬웠던 것 같다. ACM에는 "아놔 ㅅㅂ 어떻게 하라는겨" 급의 문제가 상당했지만 이번에는 일단 어떻게 접근해야 하겠다 하는 것은 거의 다 알 수 있었으니까. 우리 팀은 2/2/3 문제 이렇게 풀어서 10문제중 총 7문제를 풀었는데, 결과는 30일날 나온단다. 애초에 준비를 전혀 하지 않고 갔기 때문에 기대하지는 않는다. ㅎ

이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원
Engineering/JAVA2008/02/23 23:33


역시 Java에는 편한 것이 많다. 정규식도 좀 알아둬야 겠군...
이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원
Human Life/Sundries2008/02/09 22:20
이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원
Engineering/JAVA2008/02/04 15:00

이렇게 해도 되는 것인지 모르겠다. -_-

이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원
Engineering/JAVA2008/02/04 14:56

졸작을 하다가 네트워크 프로그래밍에서 쓰레드를 닫으려고 쓰레드의 stop()을 호출했는데, 이클립스에서 deprecated라는 경고가 떴다. 왜그런가 해서 뒤져봤더니 흠냐 -_-


Java Thread Primitive Deprecation


Why is Thread.stop deprecated?

Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.


Couldn't I just catch the ThreadDeath exception and fix the damaged object?

In theory, perhaps, but it would vastly complicate the task of writing correct multithreaded code. The task would be nearly insurmountable for two reasons:

  1. A thread can throw a ThreadDeath exception almost anywhere. All synchronized methods and blocks would have to be studied in great detail, with this in mind.
  2. A thread can throw a second ThreadDeath exception while cleaning up from the first (in the catch or finally clause). Cleanup would have to repeated till it succeeded. The code to ensure this would be quite complex.

In sum, it just isn't practical.


What about Thread.stop(Throwable)?

In addition to all of the problems noted above, this method may be used to generate exceptions that its target thread is unprepared to handle (including checked exceptions that the thread could not possibly throw, were it not for this method). For example, the following method is behaviorally identical to Java's throw operation, but circumvents the compiler's attempts to guarantee that the calling method has declared all of the checked exceptions that it may throw:

    static void sneakyThrow(Throwable t) {
        Thread.currentThread().stop(t);
    }

What should I use instead of Thread.stop?

Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized).

For example, suppose your applet contains the following start, stop and run methods:

    private Thread blinker;

    public void start() {
        blinker = new Thread(this);
        blinker.start();
    }

    public void stop() {
        blinker.stop();  // UNSAFE!
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (true) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

You can avoid the use of Thread.stop by replacing the applet's stop and run methods with:

    private volatile Thread blinker;

    public void stop() {
        blinker = null;
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (blinker == thisThread) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

How do I stop a thread that waits for long periods (e.g., for input)?

That's what the Thread.interrupt method is for. The same "state based" signaling mechanism shown above can be used, but the state change (blinker = null, in the previous example) can be followed by a call to Thread.interrupt, to interrupt the wait:

    public void stop() {
        Thread moribund = waiter;
        waiter = null;
        moribund.interrupt();
    }

For this technique to work, it's critical that any method that catches an interrupt exception and is not prepared to deal with it immediately reasserts the exception. We say reasserts rather than rethrows, because it is not always possible to rethrow the exception. If the method that catches the InterruptedException is not declared to throw this (checked) exception, then it should "reinterrupt itself" with the following incantation:

    Thread.currentThread().interrupt();

This ensures that the Thread will reraise the InterruptedException as soon as it is able.


What if a thread doesn't respond to Thread.interrupt?

In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.


Why are Thread.suspend and Thread.resume deprecated?

Thread.suspend is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes.


What should I use instead of Thread.suspend and Thread.resume?

As with Thread.stop, the prudent approach is to have the "target thread" poll a variable indicating the desired state of the thread (active or suspended). When the desired state is suspended, the thread waits using Object.wait. When the thread is resumed, the target thread is notified using Object.notify.

For example, suppose your applet contains the following mousePressed event handler, which toggles the state of a thread called blinker:

    private boolean threadSuspended;

    Public void mousePressed(MouseEvent e) {
        e.consume();

        if (threadSuspended)
            blinker.resume();
        else
            blinker.suspend();  // DEADLOCK-PRONE!

        threadSuspended = !threadSuspended;
    }

You can avoid the use of Thread.suspend and Thread.resume by replacing the event handler above with:

    public synchronized void mousePressed(MouseEvent e) {
        e.consume();

        threadSuspended = !threadSuspended;

        if (!threadSuspended)
            notify();
    }

and adding the following code to the "run loop":

                synchronized(this) {
                    while (threadSuspended)
                        wait();
                }

The wait method throws the InterruptedException, so it must be inside a try ... catch clause. It's fine to put it in the same clause as the sleep. The check should follow (rather than precede) the sleep so the window is immediately repainted when the the thread is "resumed." The resulting run method follows:

    public void run() {
        while (true) {
            try {
                Thread.currentThread().sleep(interval);

                synchronized(this) {
                    while (threadSuspended)
                        wait();
                }
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

Note that the notify in the mousePressed method and the wait in the run method are inside synchronized blocks. This is required by the language, and ensures that wait and notify are properly serialized. In practical terms, this eliminates race conditions that could cause the "suspended" thread to miss a notify and remain suspended indefinitely.

While the cost of synchronization in Java is decreasing as the platform matures, it will never be free. A simple trick can be used to remove the synchronization that we've added to each iteration of the "run loop." The synchronized block that was added is replaced by a slightly more complex piece of code that enters a synchronized block only if the thread has actually been suspended:

                if (threadSuspended) {
                    synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }
                }

In the absence of explicit synchronization, threadSuspended must be made volatile to ensure prompt communication of the suspend-request.

The resulting run method is:

    private boolean volatile threadSuspended;

    public void run() {
        while (true) {
            try {
                Thread.currentThread().sleep(interval);

                if (threadSuspended) {
                    synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }
                }
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

Can I combine the two techniques to produce a thread that may be safely "stopped" or "suspended"?

Yes; it's reasonably straightforward. The one subtlety is that the target thread may already be suspended at the time that another thread tries to stop it. If the stop method merely sets the state variable (blinker) to null, the target thread will remain suspended (waiting on the monitor), rather than exiting gracefully as it should. If the applet is restarted, multiple threads could end up waiting on the monitor at the same time, resulting in erratic behavior.

To rectify this situation, the stop method must ensure that the target thread resumes immediately if it is suspended. Once the target thread resumes, it must recognize immediately that it has been stopped, and exit gracefully. Here's how the resulting run and stop methods look:

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (blinker == thisThread) {
            try {
                thisThread.sleep(interval);

                synchronized(this) {
                    while (threadSuspended && blinker==thisThread)
                        wait();
                }
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

    public synchronized void stop() {
        blinker = null;
        notify();
    }

If the stop method calls Thread.interrupt, as described above, it needn't call notify as well, but it still must be synchronized. This ensures that the target thread won't miss an interrupt due to a race condition.


What about Thread.destroy?

Thread.destroy has never been implemented. If it were implemented, it would be deadlock-prone in the manner of Thread.suspend. (In fact, it is roughly equivalent to Thread.suspend without the possibility of a subsequent Thread.resume.) We are not implementing it at this time, but neither are we deprecating it (forestalling its implementation in future). While it would certainly be deadlock prone, it has been argued that there may be circumstances where a program is willing to risk a deadlock rather than exit outright.


Why is Runtime.runFinalizersOnExit deprecated?

Because it is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. While this problem could be prevented if the class whose objects are being finalized were coded to "defend against" this call, most programmers do not defend against it. They assume that an object is dead at the time that its finalizer is called.

Further, the call is not "thread-safe" in the sense that it sets a VM-global flag. This forces every class with a finalizer to defend against the finalization of live objects!

이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원
Human Life/Prejudice2007/10/21 02:12
사용자 삽입 이미지

컴퓨터를 포맷하고 JDK를 새로 설치하였다. 역시나 환경변수 설정이 기억나지 않아 인터넷을 뒤졌다.

JDK는 도대체 왜 설치후 환경변수를 자동으로 등록하지 않는지 이해가 가지 않는다. 왜 왜 왜? 분명히 내가 모르는 이유가 있겠지? Sun사의 개발자가 귀찮아 하는 것은 아니겠지? 아니면 환경변수 그까이꺼 그거 등록해주는게 그리 어렵나? -_-;

혹시 Sun에서 MS Window를 견제하기 위해 일부러 윈도우 플랫폼에서만 이렇게 귀찮게 하게 한것은 아닐까? 이건 비약인 것 같고. 아니면 저거 등록하는 것을 잘 외우지 못하고 귀찮아하는 내가 이상한 것인가? 그것도 아닌 것 같은데...

이유를 아는 사람은 답변좀 -_-;
이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by 비회원