在另一个函数/便捷方法中,迅速获取函数的返回值

时间:2018-10-16 04:44:09

标签: swift

我想缩短代码编写便捷方法的时间,尽管我不知道如何编写一个接受值的func,然后获取在另一个函数中返回的值?在这种情况下,我要获取的值是distanceInKms

System.out.println(Test.A); // result : String B
System.out.println(Test.B); // result : String B
System.out.println(Test.getB()); // result : String B

1 个答案:

答案 0 :(得分:0)

简单,只需使用

    package com.multithread.readandwrite;

    import java.util.concurrent.Semaphore;

    public class WriteFirst {
        private static class Reader extends Thread {
            Semaphore write_waiting, read_count_mutex, use;
            volatile int readcount;

            public Reader(Semaphore write_waiting, Semaphore read_count_mutex, Semaphore use, int readcount) {
                this.write_waiting = write_waiting;
                this.read_count_mutex = read_count_mutex;
                this.use = use;
                this.readcount = readcount;
            }

            @Override
            public void run() {
                super.run();
                try {
                    write_waiting.acquire();
                    write_waiting.release();

                    read_count_mutex.acquire();
                    System.out.println(readcount);
                    if (readcount++ == 0)
                        use.acquire();
                    read_count_mutex.release();

                    // mock read
                    sleep(1);
                    System.out.println(getName());

                    read_count_mutex.acquire();
                    if (--readcount == 0)
                        use.release();
                    read_count_mutex.release();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        private static class Writer extends Thread {
            Semaphore write_count_mutex, use, write_waiting;
            volatile int write_count;

            public Writer(Semaphore write_count_mutex, Semaphore use, Semaphore write_waiting, int write_count) {
                this.write_count_mutex = write_count_mutex;
                this.use = use;
                this.write_waiting = write_waiting;
                this.write_count = write_count;
            }

            @Override
            public void run() {
                super.run();

                try {

                    write_count_mutex.acquire();
                    System.out.println(write_count);
                    if (write_count++ == 0)
                        write_waiting.acquire();
                    write_count_mutex.release();

                    use.acquire();
                    //mock write
                    sleep(1);
                    System.out.println(getName());
                    use.release();

                    write_count_mutex.acquire();
                    if (--write_count == 0)
                        write_waiting.release();
                    write_count_mutex.release();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }


        public static void main(String[] args) {

            Semaphore use = new Semaphore(1), write_count_mutex = new Semaphore(1), read_count_mutex = new Semaphore(1),
                    write_waiting = new Semaphore(1);
            int read_count = 0, write_count = 0;

            Reader[] readers = new Reader[100];
            Writer[] writers = new Writer[100];

            for (int i=0; i<100; i++) {
                readers[i] = new Reader(write_waiting, read_count_mutex, use, read_count);
                readers[i].setName("reader "+i);
                writers[i] = new Writer(write_count_mutex, use, write_waiting, write_count);
                writers[i].setName("writer "+i);
            }

            for (int i=0; i<100; i++) {
                readers[i].start();
                writers[i].start();
            }

            int i = 0;
        }
    }

let answer = Database.getDistanceInKms(currentUserLat: lat2, currentUserLong: long2, lat: lat, long: long) 方法虽然是静态的,但具有返回值,您可以直接使用它。