Skip to main content

Thread

Struct Thread 

Source
pub struct Thread { /* private fields */ }
Expand description

A thread is a single sequential flow of control within a program. It has its own call stack and program counter.

§References

Implementations§

Source§

impl Thread

Source

pub fn new(vm: &Weak<VM>, id: u64) -> Arc<Self>

Create a new thread.

Source

pub fn id(&self) -> u64

Get the identifier of the thread.

Source

pub fn vm(&self) -> Result<Arc<VM>>

Get the virtual machine that owns the thread.

§Errors

if the virtual machine cannot be accessed.

Source

pub async fn name(&self) -> String

Get the name of the thread.

Source

pub async fn set_name<S: AsRef<str>>(&self, name: S)

Set the name of the thread.

Source

pub async fn java_object(&self) -> Value

Get the Java object for this thread.

Source

pub async fn set_java_object(&self, new_java_object: Value)

Set the Java thread object for this thread.

Source

pub async fn frames(&self) -> Result<Vec<Arc<Frame>>>

Get the frames in the thread.

§Errors

if the frames cannot be accessed.

Source

pub async fn current_frame(&self) -> Result<Arc<Frame>>

Get the current frame in the thread.

§Errors

if the current frame cannot be accessed.

Source

pub fn interrupt(&self)

Set the thread as interrupted.

Source

pub fn is_interrupted(&self, clear_interrupt: bool) -> bool

Check if the thread is interrupted and clear the interrupt if specified.

Source

pub async fn sleep(&self, duration: Duration) -> bool

Sleep the thread for the specified duration. The sleep is interruptible - if another thread calls interrupt(), this method will return true to indicate the thread was interrupted, clearing the interrupt flag.

§Arguments
  • duration - The duration to sleep.
§Returns

Returns true if the sleep was interrupted, false if it completed normally.

Source

pub async fn park(&self, is_absolute: bool, time: u64) -> Result<()>

Park the thread. If the permit is available, it will be consumed and the thread will return immediately. If the permit is not available, the thread will be parked until it is unparked or the specified time has elapsed.

§Arguments
  • is_absolute - If true, the time parameter is treated as an absolute timestamp (milliseconds since epoch).
  • time - The time to park the thread. If is_absolute is true, this is the absolute timestamp in milliseconds since epoch. If is_absolute is false, this is the relative duration in nanoseconds.
§Errors

If the parking operation fails, an error will be returned.

Source

pub fn unpark(&self)

Unpark the thread if it is parked.

Source

pub fn class<'life_self, 'async_recursion, S>( &'life_self self, class_name: S, ) -> Pin<Box<dyn Future<Output = Result<Arc<Class>>> + Send + 'async_recursion>>
where S: 'async_recursion + AsRef<str> + Send, 'life_self: 'async_recursion,

Get a class and ensure it is initialized.

This implements the class initialization procedure as specified in JLS §12.4.2:

  1. If the class is already initialized, return immediately
  2. If the class is in an erroneous state, throw NoClassDefFoundError
  3. If the class is being initialized by the current thread, return (recursive initialization)
  4. If the class is being initialized by another thread, wait and recheck
  5. Mark the class as being initialized by the current thread
  6. Initialize the direct superclass first (recursive)
  7. Execute <clinit> for this class
  8. If <clinit> throws, mark as Erroneous and throw ExceptionInInitializerError
  9. Mark the class as Initialized

Note: This implementation does NOT initialize interfaces as part of class initialization unless explicitly triggered per JLS §12.4.1.

§References
§Errors

if the class cannot be loaded or initialized

Source

pub async fn invoke<C, M>( &self, class: C, method: M, parameters: &[impl RustValue], ) -> Result<Option<Value>>
where C: AsRef<str> + Send + Sync, M: AsRef<str> + Send + Sync,

Invoke a method. To invoke a method on an object reference, the object reference must be the first parameter in the parameters vector.

§Errors

if the method cannot be invoked

Source

pub async fn try_invoke<C, M>( &self, class: C, method: M, parameters: &[impl RustValue], ) -> Result<Value>
where C: AsRef<str> + Send + Sync, M: AsRef<str> + Send + Sync,

Invoke a method. To invoke a method on an object reference, the object reference must be the first parameter in the parameters vector.

§Errors

if the method cannot be invoked

Source

pub async fn execute( &self, class: &Arc<Class>, method: &Arc<Method>, parameters: &[impl RustValue], ) -> Result<Option<Value>>

Add a new frame to the thread and invoke the method. To invoke a method on an object reference, the object reference must be the first parameter in the parameters vector.

§Errors

if the method cannot be invoked.

Source

pub async fn try_execute( &self, class: &Arc<Class>, method: &Arc<Method>, parameters: &[impl RustValue], ) -> Result<Value>

Add a new frame to the thread and invoke the method. To invoke a method on an object reference, the object reference must be the first parameter in the parameters vector.

§Errors

if the method cannot be invoked.

Source

pub async fn object<C, M>( &self, class_name: C, descriptor: M, parameters: &[impl RustValue], ) -> Result<Value>
where C: AsRef<str> + Send + Sync, M: AsRef<str> + Send + Sync,

Create a new VM Object by invoking the constructor of the specified class.

§Errors

if the object cannot be created

Trait Implementations§

Source§

impl Debug for Thread

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Thread for Thread

Source§

type Vm = VM

The concrete VM type.
Source§

type Frame = Frame

The concrete stack frame type.
Source§

fn id(&self) -> u64

Get the identifier of the thread.
Source§

fn vm(&self) -> Result<Arc<VM>>

Get the virtual machine that owns the thread. Read more
Source§

fn name(&self) -> BoxFuture<'_, String>

Get the name of the thread.
Source§

fn set_name<'a>(&'a self, name: &'a str) -> BoxFuture<'a, ()>

Set the name of the thread.
Source§

fn java_object(&self) -> BoxFuture<'_, Value>

Get the Java object associated with this thread.
Source§

fn set_java_object(&self, value: Value) -> BoxFuture<'_, ()>

Set the Java object associated with this thread.
Source§

fn frames(&self) -> BoxFuture<'_, Result<Vec<Arc<Frame>>>>

Get the call stack frames. Read more
Source§

fn interrupt(&self)

Interrupt the thread.
Source§

fn is_interrupted(&self, clear_interrupt: bool) -> bool

Check if the thread is interrupted.
Source§

fn sleep(&self, duration: Duration) -> BoxFuture<'_, bool>

Sleep the thread for the specified duration. Returns true if the sleep was interrupted.
Source§

fn park(&self, is_absolute: bool, time: u64) -> BoxFuture<'_, Result<()>>

Park the thread. Read more
Source§

fn unpark(&self)

Unpark the thread.
Source§

fn class<'a>(&'a self, class_name: &'a str) -> BoxFuture<'a, Result<Arc<Class>>>

Load, link, and initialize a class by name. Read more
Load and link a class by name without initializing it. Read more
Source§

fn register_class(&self, class: Arc<Class>) -> BoxFuture<'_, Result<()>>

Register a class with the class loader. Read more
Source§

fn invoke<'a>( &'a self, class: &'a str, method: &'a str, parameters: &'a [Value], ) -> BoxFuture<'a, Result<Option<Value>>>

Invoke a method by class name and method signature. Read more
Source§

fn try_invoke<'a>( &'a self, class: &'a str, method: &'a str, parameters: &'a [Value], ) -> BoxFuture<'a, Result<Value>>

Invoke a method, returning an error if no value is returned. Read more
Source§

fn execute<'a>( &'a self, class: &'a Arc<Class>, method: &'a Arc<Method>, parameters: &'a [Value], ) -> BoxFuture<'a, Result<Option<Value>>>

Execute a method on a class. Read more
Source§

fn try_execute<'a>( &'a self, class: &'a Arc<Class>, method: &'a Arc<Method>, parameters: &'a [Value], ) -> BoxFuture<'a, Result<Value>>

Execute a method, returning an error if no value is returned. Read more
Source§

fn object<'a>( &'a self, class_name: &'a str, descriptor: &'a str, parameters: &'a [Value], ) -> BoxFuture<'a, Result<Value>>

Create a new VM object by invoking a constructor. Read more
Source§

fn intern_string<'a>(&'a self, string: &'a str) -> BoxFuture<'a, Result<Value>>

Intern a string, returning the associated Java String value. Read more

Auto Trait Implementations§

§

impl !Freeze for Thread

§

impl !RefUnwindSafe for Thread

§

impl Send for Thread

§

impl Sync for Thread

§

impl Unpin for Thread

§

impl !UnwindSafe for Thread

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more