Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中如何换行_Java - Fatal编程技术网

java中如何换行

java中如何换行,java,Java,我正在使用\n作为线程中的换行符,但它不起作用。我看了很多答案,建议使用 System.getPropertyline.separator; 但这对我也不起作用 public static void main(String args[]) { final ReentrantLock lock1 = new ReentrantLock(); final ReentrantLock lock2 = new ReentrantLock(); Runnable try1_2 = getRunnabl

我正在使用\n作为线程中的换行符,但它不起作用。我看了很多答案,建议使用 System.getPropertyline.separator; 但这对我也不起作用

public static void main(String args[]) {

final ReentrantLock lock1 = new ReentrantLock();
final ReentrantLock lock2 = new ReentrantLock();

Runnable try1_2 = getRunnable(lock1, "lock 1", lock2, "lock 2");
Runnable try2_1 = getRunnable(lock2, "lock 2", lock1, "lock 1");

new Thread(try1_2).start();
new Thread(try2_1).start();

}

private Runnable getRunnable(final ReentrantLock lock1, final String lock1Name, final ReentrantLock lock2, final String lock2Name) {
    return new Runnable() {
        @Override
        public void run() {
            try {

                if (lock1.tryLock(1, TimeUnit.SECONDS)) {

                    System.out.println("1"+lock1Name + " acquired in thread " + Thread.currentThread().getName());
                    text_3.setText(text_3.getText() +"1"+lock1Name + " acquired in thread " + Thread.currentThread().getName());

                    if (lock2.tryLock(1, TimeUnit.SECONDS)) {

                        System.out.println("2"+lock2Name + " acquired in thread " + Thread.currentThread().getName());
                        text_3.setText(text_3.getText() + "\n"+"2"+lock2Name + " acquired in thread " + Thread.currentThread().getName());

                        Thread.sleep(2000);
                    } else {

                        System.out.println("3"+"Could not acquire "+lock2Name + " in thread " + Thread.currentThread().getName());
                        text_3.setText(text_3.getText() + "\n"+"3"+"Could not acquire "+lock2Name + " in thread " + Thread.currentThread().getName());

                        lock1.unlock();

                        System.out.println("4"+lock1Name + " released in thread " + Thread.currentThread());
                        text_3.setText(text_3.getText() + "\n"+"4"+lock1Name + " released in thread " + Thread.currentThread());

                    }
                } else {
                    System.out.println("5"+"Could not acquire " + lock1Name + " in thread " + Thread.currentThread());
                    text_3.setText(text_3.getText() + "\n"+"5"+"Could not acquire " + lock1Name + " in thread " + Thread.currentThread());
                }
            } catch (InterruptedException e) {

            } finally {

                if (lock1.isHeldByCurrentThread()) lock1.unlock();
                if (lock2.isHeldByCurrentThread()) lock2.unlock();
            }
        }
    };
}
结果如下:

1在线程线程1中获取的锁2 1在线程线程0中获取的锁2

我希望情况如下:

1线程1中获取的锁存2
1在thread thread-0中获取的锁2….

您是否尝试设置文本,因为这是测试?

请使用System.getPropertyline.separator;像这样,

 String newLine = System.getProperty("line.separator");

 //then insert newLine variable as this :
 text_3.setText(text_3.getText() + newLine+ "2 " + lock2Name +
 "acquired in thread " + Thread.currentThread().getName());

什么不起作用?请给出实际输出和预期输出的例子,你能找出引起故障的代码吗?我用刹车代替了刹车。如果这不是您的意图,请回滚。猜测->您是否在html中显示文本_3?在jframe中不显示。text_3是jlabely它在JLabel上不起作用。我将JLabel更改为TextArea..现在它起作用了。