Example usage for java.lang Thread currentThread

List of usage examples for java.lang Thread currentThread

Introduction

In this page you can find the example usage for java.lang Thread currentThread.

Prototype

@HotSpotIntrinsicCandidate
public static native Thread currentThread();

Source Link

Document

Returns a reference to the currently executing thread object.

Usage

From source file:ThreadDemo.java

public static void main(String args[]) {
    Thread currThread = Thread.currentThread();
    // thread created
    Thread t = new Thread(new ThreadDemo(), "java2s.com thread");

    System.out.println("current thread = " + currThread);
    System.out.println("thread created = " + t);
    // this will call run() function
    t.start();//from  w ww  .  j ava 2  s. c  o m
}

From source file:Main.java

public static void main(String... args) {
    //Anonymous ways
    Runnable r1 = new Runnable() {
        public void run() {
            System.out.println("Hello world from " + Thread.currentThread().getName());
        }/*from   w w  w  . j a  v a2  s. c o  m*/
    };

    // Runnable is already a functional interface
    Runnable r2 = () -> System.out.println("Hello world  from " + Thread.currentThread().getName());
    (new Thread(r1)).start();
    (new Thread(r2)).start();

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w ww. ja va 2 s  . co  m
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                System.out.println("Hello World on " + Thread.currentThread());
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Finished on " + Thread.currentThread());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");

    strings.parallelStream().filter(s -> {
        System.out.format("filter:  %s [%s]\n", s, Thread.currentThread().getName());
        return true;
    }).map(s -> {/*  w ww .  j a va 2 s  .  c o  m*/
        System.out.format("map:     %s [%s]\n", s, Thread.currentThread().getName());
        return s.toUpperCase();
    }).forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");

    strings.parallelStream().filter(s -> {
        System.out.format("filter:  %s [%s]\n", s, Thread.currentThread().getName());
        return true;
    }).map(s -> {/*w  ww .  j  av a2s .c  om*/
        System.out.format("map:     %s [%s]\n", s, Thread.currentThread().getName());
        return s.toUpperCase();
    }).sorted((s1, s2) -> {
        System.out.format("sort:    %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
        return s1.compareTo(s2);
    }).forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}

From source file:Resource.java

/**
 * @param args//from  w  w w.  j a  v a 2  s.  c  o m
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("com/sap/ca/aspose/license/Aspose.Total.Java.lic");
    //InputStream is = cl.getResourceAsStream("com/sap/ca/aspose/license/a.txt");
    if (is == null) {
        System.err.println("Error is is null");
        System.exit(1);

    }
    String s;
    try {
        s = IOUtils.toString(is);
        System.out.println(s);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String args[]) {
    Thread t = Thread.currentThread();
    t.setName("My Thread");
    t.setPriority(1);//from   ww  w. ja v a 2 s.  co m
    System.out.println("current thread: " + t);
    int active = Thread.activeCount();
    System.out.println("currently active threads: " + active);
    Thread all[] = new Thread[active];
    Thread.enumerate(all);
    for (int i = 0; i < active; i++) {
        System.out.println(i + ": " + all[i]);
    }
    Thread.dumpStack();
}

From source file:Main.java

public static void main(String[] arg) throws Exception {
    Runnable parallelCode = () -> {
        HashSet<String> allThreads = new HashSet<>();
        IntStream.range(0, 1_000_000).parallel().filter(i -> {
            allThreads.add(Thread.currentThread().getName());
            return false;
        }).min();/*from  www .  ja v  a2 s.  c  o m*/
        System.out.println("executed by " + allThreads);
    };
    System.out.println("default behavior: ");
    parallelCode.run();
    System.out.println("specialized pool:");
    ForkJoinPool pool = new ForkJoinPool(2);
    pool.submit(parallelCode).get();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runnable myRunnable = new Runnable() {
        @Override/*from   w ww.j  a v  a2s.c o  m*/
        public void run() {
            try {
                System.out.println("Start: " + Thread.currentThread().getName());
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    };
    Thread one = new Thread(myRunnable);
    Thread two = new Thread(myRunnable);
    one.start();
    two.start();

    List<Thread> threads = getThreadsFor(myRunnable);
    for (Thread thread : threads)
        System.out.println("Found: " + thread.getName());
}

From source file:Main.java

public static void main(String[] args) {
    double sum = Employee.persons().stream().reduce(0.0, (Double partialSum, Employee p) -> {
        double accumulated = partialSum + p.getIncome();
        System.out.println(Thread.currentThread().getName() + "  - Accumulator: partialSum  = " + partialSum
                + ",  person = " + p + ", accumulated = " + accumulated);
        return accumulated;
    }, (a, b) -> {/*w w  w  .j a v  a2s .c  o  m*/
        double combined = a + b;
        System.out.println(Thread.currentThread().getName() + "  - Combiner:  a  = " + a + ", b  = " + b
                + ", combined  = " + combined);
        return combined;
    });
    System.out.println("--------------------------------------");
    System.out.println(sum);

    sum = Employee.persons().parallelStream().reduce(0.0, (Double partialSum, Employee p) -> {
        double accumulated = partialSum + p.getIncome();
        System.out.println(Thread.currentThread().getName() + "  - Accumulator: partialSum  = " + partialSum
                + ",  person = " + p + ", accumulated = " + accumulated);
        return accumulated;
    }, (a, b) -> {
        double combined = a + b;
        System.out.println(Thread.currentThread().getName() + "  - Combiner:  a  = " + a + ", b  = " + b
                + ", combined  = " + combined);
        return combined;
    });
    System.out.println(sum);
}