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:Main.java

public static void main(String[] args) {
    Thread t = Thread.currentThread();
    Main t1 = new Main(3);
    t1.createThread("Child 1");
    t1.createThread("Child 2");
    for (int i = 0; i <= 5; i++) {
        try {//from w  ww . j a  v  a2  s.c o m
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("The cirrent Thread is " + t.getName() + " and thread ID is " + t.getId());
    }
}

From source file:CatchAllThreadExceptionHandler.java

public static void main(String[] args) {
    CatchAllThreadExceptionHandler handler = new CatchAllThreadExceptionHandler();

    // Set an uncaught exception handler for main thread
    Thread.currentThread().setUncaughtExceptionHandler(handler);

    // Throw an exception
    throw new RuntimeException();
}

From source file:Main.java

public static void main(String[] args) {
    System.setProperty("java.security.policy", "file:/C:/java.policy");

    Main sm = new Main();

    System.setSecurityManager(sm);

    // check if accepting access for thread is enabled
    sm.checkAccess(Thread.currentThread());

    System.out.println("Allowed!");
}

From source file:Main.java

public static void main(String[] args) {
    Main ct1 = new Main("First Thread");
    Main ct2 = new Main("Second Thread");
    ct1.start();/*from  w ww .j a  v a  2s . co  m*/
    ct2.start();

    Thread t = Thread.currentThread();
    String threadName = t.getName();
    System.out.println("Inside  main() method:  " + threadName);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Integer ageSum = persons.parallelStream().reduce(0, (sum, p) -> {
        System.out.format("accumulator: sum=%s; person=%s; thread=%s\n", sum, p,
                Thread.currentThread().getName());
        return sum += p.age;
    }, (sum1, sum2) -> {/*from   w  ww  .  j  a v a 2 s  . c  o  m*/
        System.out.format("combiner: sum1=%s; sum2=%s; thread=%s\n", sum1, sum2,
                Thread.currentThread().getName());
        return sum1 + sum2;
    });

    System.out.println(ageSum);
}

From source file:clicker.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);

    lo.start();/* w  w w  .  java  2s .  c o  m*/
    hi.start();
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }

    lo.stop();
    hi.stop();

    // Wait for child threads to terminate.
    try {
        hi.t.join();
        lo.t.join();
    } catch (InterruptedException e) {
        System.out.println("InterruptedException caught");
    }

    System.out.println("Low-priority thread: " + lo.click);
    System.out.println("High-priority thread: " + hi.click);
}

From source file:MyThread.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    MyThread hi = new MyThread(Thread.NORM_PRIORITY + 2);
    MyThread lo = new MyThread(Thread.NORM_PRIORITY - 2);
    lo.start();/*  w  ww .j a v  a2s  .  c o  m*/
    hi.start();

    try {
        Thread.sleep(10000);
    } catch (Exception e) {
    }

    lo.stop();
    hi.stop();
    System.out.println(lo.click + " vs. " + hi.click);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main jTextPaneApp = new Main();
    jTextPaneApp.setVisible(true);/*  w ww  .j ava2 s.c  o m*/
    Thread.currentThread().sleep(1000);
    jTextPaneApp.change();
}

From source file:Test.java

public static void main(String[] args) {
    final AtomicLong orderIdGenerator = new AtomicLong(0);
    final List<Item> orders = Collections.synchronizedList(new ArrayList<Item>());

    for (int i = 0; i < 10; i++) {
        Thread orderCreationThread = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    long orderId = orderIdGenerator.incrementAndGet();
                    Item order = new Item(Thread.currentThread().getName(), orderId);
                    orders.add(order);/* w  w  w . ja v  a 2s. co m*/
                }
            }
        });
        orderCreationThread.setName("Order Creation Thread " + i);
        orderCreationThread.start();
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Set<Long> orderIds = new HashSet<Long>();
    for (Item order : orders) {
        orderIds.add(order.getID());
        System.out.println("Order id:" + order.getID());
    }
}

From source file:GetPriority.java

public static void main(String[] args) {
    System.out.println(//from w w w.  jav a  2 s  .  c o  m
            "in main() - Thread.currentThread().getPriority()=" + Thread.currentThread().getPriority());

    System.out.println("in main() - Thread.currentThread().getName()=" + Thread.currentThread().getName());

    Thread threadA = new Thread(makeRunnable(), "threadA");
    threadA.start();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException x) {
    }

    System.out.println("in main() - threadA.getPriority()=" + threadA.getPriority());
}